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: -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Mar 1, 6:22 pm, zindelo <sh...-+aa7D19H+JlBDgjK7y7TUQ@public.gmane.org> wrote:> Using the rails guides has_many :through example > athttp://guides.rubyonrails.org/association_basics.html#the-has_many-th... > , 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:-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 1, 11:13 pm, zindelo <sh...-+aa7D19H+JlBDgjK7y7TUQ@public.gmane.org> 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 makesUsage 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.> better sense. Thanks-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 %> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.