I''m a Ruby and Rails newbie, trying to get out of PHP. I have two AR models, Trip and Activity, where Trip has_many Activities. I want to pull all the trips and load their associated activities as attributes so that I can iterate over the trips, displaying each one and it''s activities like so: <% for trip in @trips %> <h3><%= trip.name%> </h3> <p><em><%= trip.summary%></em> <ul> <% for activity in trip.activities %> <li><strong><%= activity.name %></strong> <br> <%= @activity.description %></li> <% end %> </ul> <% end %> I was able to hack this together in my controller: @trips = Trip.find(:all) @trips.each do |trip| a = Activity.find_all_by_trip_id(trip.id) a.each { |b| c = b.trip_id } trip.activities = a if trip.id == c end It seems to load everything the way it should be, but I can''t get it to display correctly, and there''s got to be a cleaner way to write this. Any help would be greatly appreciated. Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2008-Apr-23 19:10 UTC
Re: Multi-dimensional hash/array as model attribute
> I''m a Ruby and Rails newbie, trying to get out of PHP. > > I have two AR models, Trip and Activity, where Trip has_many Activities. > > I want to pull all the trips and load their associated activities as > attributes so that I can iterate over the trips, displaying each one and > it''s activities like so: > > <% for trip in @trips %> > <h3><%= trip.name%> </h3> > <p><em><%= trip.summary%></em> > <ul> > <% for activity in trip.activities %> > <li><strong><%= activity.name %></strong> <br> > <%= @activity.description %></li> > <% end %> > </ul> > <% end %> > > > I was able to hack this together in my controller: > > @trips = Trip.find(:all) > > @trips.each do |trip| > a = Activity.find_all_by_trip_id(trip.id) > a.each { |b| c = b.trip_id } > trip.activities = a if trip.id == c > end > > It seems to load everything the way it should be, but I can''t get it to > display correctly, and there''s got to be a cleaner way to write this.Assuming your tables are set up correctly (that is activities.trip_id exists) you shouldn''t need anything more than the following in your controller. That whole second block is redundant. @trips = Trip.find(:all) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
OK. I was able to get it to display correctly by changing "trip.activities" to "trip[:activities]" in the view. I don''t quite understand why that worked though... And there''s still the issue of the controller code. There must be a Ruby way to write that. Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller
2008-Apr-23 19:19 UTC
Re: Multi-dimensional hash/array as model attribute
sorry, but i don''t exactly get your problem...> <% for trip in @trips %> > <h3><%= trip.name%> </h3> > <p><em><%= trip.summary%></em> > <ul> > <% for activity in trip.activities %> > <li><strong><%= activity.name %></strong> <br> > <%= @activity.description %></li> > <% end %> > </ul> > <% end %>this looks good so far, but for a small typo in <%= @activity.description %></li> which should be: <%= activity.description %></li>> > @trips = Trip.find(:all)ok and all you need> > @trips.each do |trip| > a = Activity.find_all_by_trip_id(trip.id) > a.each { |b| c = b.trip_id } > trip.activities = a if trip.id == c > enddon''t know, what you try to do here? in your Trip model you have defined: has_many :activities in Activities model: belongs_to :trip then> @trips = Trip.find(:all)is all you need to do and your code will run, activities are loaded when needed or do: @trips = Trip.find(:all, :include => [:activities]) which would give slightly better db perdormance -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:> @trips = Trip.find(:all, :include => [:activities]) > which would give slightly better db perdormancePhilip Hallstrom wrote:> Assuming your tables are set up correctly (that is activities.trip_id > exists) you shouldn''t need anything more than the following in your > controller. That whole second block is redundant. > > @trips = Trip.find(:all)Thank you both for your replies! Damn!!! I think I''m in love with Rails! I have spent the past 5 hours trying to get this to work. I had no idea it was that simple. I thought I had to load the activities separately, *then* associate them with their parent trip with this whole multi-dimensional crap as I would have in PHP. I feel stupid AND smarter now. Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Welcome to Rails! ;-) You''ve just described us all. -Danimal On Apr 23, 1:27 pm, Jed Foster <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I feel stupid AND smarter now. 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-/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 -~----------~----~----~----~------~----~------~--~---