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%> &nbsp; &nbsp; </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.

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%> &nbsp; &nbsp; </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)

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.

Thorsten Mueller wrote:

@trips = Trip.find(:all, :include => [:activities]) which would give slightly better db perdormance

Philip 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!

Welcome to Rails! :wink:

You've just described us all.

-Danimal