Rails guides has_many :through example

Using the rails guides has_many :through example at Active Record Associations — Ruby on Rails Guides , what is the best approach to writing the nested model form that would go with the example?

Physician

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

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| %>

<p>

	<%= f.label :name %><br />

	<%= f.text_field :name %>

</p>

<hr>

<h3>Patient appointments:</h3>

<table>

	<tr><th>Patient Name</th><th>Appointment Date</th><th></th></tr>

	**<% appt_index = 0 %>**

	<%= f.fields_for :appointments do |appointment| %>

		<tr>

			<td>

				**<%= @physician.appointments[appt_index].patient.name %> **

			</td>

			<td>

				%= appointment.text_field :appointment_date %>

			</td>

		</tr>

		**<% appt_index = appt_index + 1 %>**

	<% end %>

</table>

<hr>

<p><%= f.submit %></p>

<% end %>