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 %>
<%= trip.name%>
<%= trip.summary%>
<% for activity in trip.activities %>
-
<%= activity.name %>
<%= @activity.description %>
<% end %>
<% 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.
<%= trip.summary%>
@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 M. wrote:
@trips = Trip.find(:all, :include => [:activities])
which would give slightly better db perdormance
Philip H. 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!
sorry, but i don’t exactly get your problem…
<% for trip in @trips %>
<%= trip.name%>
<%= trip.summary%>
<% for activity in trip.activities %>
-
<%= activity.name %>
<%= @activity.description %>
<% end %>
<% end %>
this looks good so far, but for a small typo in
<%= @activity.description %>
which should be:
<%= activity.description %>
@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
end
don’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
Welcome to Rails!
You’ve just described us all.
-Danimal
On Apr 23, 1:27 pm, Jed F. [email protected]