Rails guides has_many :through example

Using the rails guides has_many :through example
at
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
, what is the best approach to writing the nested model form that would
go
with the example?

Physician

Name:

Physician Appointments

Patient Name:Appointment Date:

What are you trying to accomplish here? has_many :through setup
requires three tables. In this case it seems all you need is physician
has_many appointments.

class Physician < ActiveRecord::Base
has_many :appointments
end

class Appointment < ActiveRecord::Base
belongs_to :physician
end

And the phyicians and appointments tables will have the corresponding
name and datetime attributes.

-S

Sorry I didn’t add the patients in there, but I would need the three
tables.
What I’m after is how to represent this data in a form. There is a ton
of
info out there about associations, but not much related to using the
data in
a join table like the appointment_date field. It seems like the
physician
form and the patient form would be very similar and both are needed,
storing
the appointment_date in the join table so both can use.

If you use a fields_for to list out the appointments using
:appointments,
how do you reference to the patient name? The patient name doesn’t need
to
be editable, but the only field I can access is user_id from the
appointments table and not name from the patients table. Hope that makes
better sense. Thanks

On Mar 1, 11:13pm, zindelo [email protected] wrote:

Sorry I didn’t add the patients in there, but I would need the three tables.
What I’m after is how to represent this data in a form. There is a ton of
info out there about associations, but not much related to using the data in
a join table like the appointment_date field. It seems like the physician
form and the patient form would be very similar and both are needed, storing
the appointment_date in the join table so both can use.

It doesn’t sound like you need a has_many :through setup here for
Physician at least. One way to set this up is Phyisician has_many
Patients and Physician has _many Appointments. Appointments and
Patients both belong_to Physician. You could add that Patient has_one
or has_many Appointment through Physician.

If you use a fields_for to list out the appointments using :appointments,
how do you reference to the patient name? The patient name doesn’t need to
be editable, but the only field I can access is user_id from the
appointments table and not name from the patients table. Hope that makes

Usage of fields_for is fairy well documented. You should look at the
usage of accepts_nested_attributes_for directive for editing nested
records on the same form. If you want to do something dynamically on
the client side with nested records a good article is here
http://railsforum.com/viewtopic.php?id=28447.

has_many :through is needed. In order for appointment_date to be
available
to both physicians and patients, it should be located in the
appointments
table.

Using fields_for :appointments is quite simple and I successfully list
out
the appointments for a physician or patient. Listing out the associated
patient name or physician name is where I’m asking for help. I can list
out
the physician_id or the patient_id from the appointments table, but not
their name.

Thanks

I’ve solved the issue, but it sure seems like a hack. Hopefully someone
can
help with a more elegant approach. It seems like I’m missing something
simple, all I need to do is display a name.

<%= form_for @physician do |f| %>

<%= f.label :name %>
<%= f.text_field :name %>


Patient appointments:

*<% appt_index = 0 %>* <%= f.fields_for :appointments do |appointment| %> *<% appt_index = appt_index + 1 %>* <% end %>
Patient Name Appointment Date
*<%= @physician.appointments[appt_index].patient.name %> * %= appointment.text_field :appointment_date %>

<%= f.submit %>

<% end %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs