Let me start off by saying I am very much a beginner. I currently have 2 models - Person, and address. I am currently trying to display a person and their address on the "index" page in the person section. I can create a person and multiple addresses all at once using the Advanced Rails Recipe, and the foreign key saves fine, but now I am trying to get the person to display with their associated address on the index page and am lost. <% if @people -%> <script type="text/javascript"> var people = <%= @people.to_json %>; </script> <% end -%> The above code spits out the proper string for all the people I want, however, I can''t seem to figure out how to spit out their addresses as well. def index @people = Person.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @people } end end The above is what I have in my person controller currently, and I have no idea where to go from here to get this done. Any help would be greatly appreciated. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Assuming you have set up relationships correctly either has_one or has_many between Person and Address. It should be as simple as @person.address (or @person.address1 etc.). The @person instance variable will carry the associated data with it so you simply have to use the "." operator to get the address data. Bharat --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
.to_json takes an :include param--have a look at the activerecord docs. -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Jeff.Corcoran Sent: Sunday, July 20, 2008 3:01 PM To: Ruby on Rails: Talk Subject: [Rails] Displaying hash for has_many Let me start off by saying I am very much a beginner. I currently have 2 models - Person, and address. I am currently trying to display a person and their address on the "index" page in the person section. I can create a person and multiple addresses all at once using the Advanced Rails Recipe, and the foreign key saves fine, but now I am trying to get the person to display with their associated address on the index page and am lost. <% if @people -%> <script type="text/javascript"> var people = <%= @people.to_json %>; </script> <% end -%> The above code spits out the proper string for all the people I want, however, I can''t seem to figure out how to spit out their addresses as well. def index @people = Person.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @people } end end The above is what I have in my person controller currently, and I have no idea where to go from here to get this done. Any help would be greatly appreciated. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---