M
M
Max2015-11-27 02:35:23
Ruby on Rails
Max, 2015-11-27 02:35:23

How to link 2 tables in Rails?

Hello, there are 2 tables.
1. Doctor
's table 2. Patient's table
Abbreviated as follows

create_table "doctors", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "doctor_first_name"
t.string "doctor_second_name"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "surname"
t.integer "age"
t.boolean "status"
t.date "enter_date"
t.text "diagnoz"
t.integer "doctor_id"
t.string "doctor_first_name"
t.string "doctor_second_name"
end

Only a doctor can add a new patient. Therefore, if there are many doctors and many patients, I want to display a table of all patients and in one of the columns indicate the name of the treating doctor.
The summary output looks like this
<td><%= user.name %></td>
<td><%= user.surname %></td>
<td><%= user.age %></td>
<% if user.status == true %>
<td>Здоровый</td>
<% else %>
<td>Больной</td>
<% end %>
<td><%= user.enter_date %></td>
<td><%= user.doctor_first_name %></td>
<td><%= user.doctor_id %></td>
<td><%= user.diagnoz %></td>

At the output, we get a finished table, but with gaps in place of the doctor_first_name column. That is, for example, doctor_id (ID of the doctor who added the patient) is displayed correctly, but doctor_first_name is skipped??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
N. Bekseitov, 2015-11-27
@maxprof

not user.doctor_first_name, but user.doctor.doctor_first_name
and you need to specify a link belongs_to :doctorfor the user model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question