# Multi-dimensional hash/array as model attribute

**URL:** <https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645>\
**Category:** Rails\
**Created:** [April 23, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645 "2008-04-23T21:07:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jedfoster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jedfoster](https://www.ruby-forum.com/u/jedfoster)\
**Post date:** [April 23, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645/1 "2008-04-23T21:07:01Z")

</div>

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

_\<%= 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.

---

<div class="post-metadata">

**Author:** ![jedfoster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jedfoster](https://www.ruby-forum.com/u/jedfoster)\
**Post date:** [April 23, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645/2 "2008-04-23T21:12:04Z")

</div>

> _\<%= 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)

---

<div class="post-metadata">

**Author:** ![jedfoster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jedfoster](https://www.ruby-forum.com/u/jedfoster)\
**Post date:** [April 23, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645/3 "2008-04-23T21:15:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jedfoster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jedfoster](https://www.ruby-forum.com/u/jedfoster)\
**Post date:** [April 23, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645/4 "2008-04-23T21:27:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jedfoster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jedfoster](https://www.ruby-forum.com/u/jedfoster)\
**Post date:** [April 23, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645/5 "2008-04-23T21:19:55Z")

</div>

sorry, but i don’t exactly get your problem…

> \<% for trip in @trips %\>
> 
> ### \<%= trip.name%\> &nbsp; &nbsp;
> 
> _\<%= 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

---

<div class="post-metadata">

**Author:** ![jedfoster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jedfoster](https://www.ruby-forum.com/u/jedfoster)\
**Post date:** [April 24, 2008, 2:13am UTC](https://www.ruby-forum.com/t/multi-dimensional-hash-array-as-model-attribute/135645/6 "2008-04-24T02:13:37Z")

</div>

Welcome to Rails! 😉

You’ve just described us all.

-Danimal

On Apr 23, 1:27 pm, Jed F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
