# Merging multiple AR result hashes into one for view loop

**URL:** <https://www.ruby-forum.com/t/merging-multiple-ar-result-hashes-into-one-for-view-loop/89848>\
**Category:** Rails\
**Created:** [March 23, 2007, 9:45am UTC](https://www.ruby-forum.com/t/merging-multiple-ar-result-hashes-into-one-for-view-loop/89848 "2007-03-23T09:45:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [March 23, 2007, 9:45am UTC](https://www.ruby-forum.com/t/merging-multiple-ar-result-hashes-into-one-for-view-loop/89848/1 "2007-03-23T09:45:03Z")

</div>

Need some opinions about the best way to go about this. I  
know :include does something similar, though I’m not sure if thats  
what I need.

I have a Users model which :has\_many of several other models, we’ll  
call them model1, model2, model3 for example.

As of right now I have something like this in my controller:

@result1 = User.model1  
@result2 = User.model2  
@result3 = User.model3

Now the various models that belong to User have various different  
columns/attributes in them, but they do share a common attribute of a  
time column.

For use in one of my view I want to, for example, combine @result1,  
@result2 and @result3 in one main @results hash and order them all by  
time. Then in my view I can do one simple loop such as:

for result in @results  
result.attribute\_from\_model1  
result.attribute\_from\_model2\_that\_isnt\_in\_model1  
…  
end

I suppose there is a way to do this with either a Ruby (zip?) hash  
merging method or a single AR call/join. Any thoughts? Thanks.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [March 23, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/merging-multiple-ar-result-hashes-into-one-for-view-loop/89848/2 "2007-03-23T15:08:51Z")

</div>

Also I was thinking maybe a combo of :include and :join might work? I  
can’t seem to get the syntax right.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [March 23, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/merging-multiple-ar-result-hashes-into-one-for-view-loop/89848/3 "2007-03-23T16:21:05Z")

</div>

The has\_many collections aren’t hashes, they’re arrays. You can  
concatenate arrays with a + like so:

for result in User.model1 + User.model2 + User.model3  
…  
end

If you’re worried about their sort order, you can sort them manually  
with the Array#sort\_by method.

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [March 23, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/merging-multiple-ar-result-hashes-into-one-for-view-loop/89848/4 "2007-03-23T17:03:12Z")

</div>

Bryan,

Ah ok, arrays make them much easier to deal with. Thanks I’ll give  
this a try.

On Mar 23, 4:21 pm, Bryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![marstoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@marstoni](https://www.ruby-forum.com/u/marstoni)\
**Post date:** [March 23, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/merging-multiple-ar-result-hashes-into-one-for-view-loop/89848/5 "2007-03-23T18:00:56Z")

</div>

Marston A. wrote:

> Need some opinions about the best way to go about this. I  
> know :include does something similar, though I’m not sure if thats  
> what I need.
> 
> I have a Users model which :has\_many of several other models, we’ll  
> call them model1, model2, model3 for example.
> 
> As of right now I have something like this in my controller:
> 
> @result1 = User.model1  
> @result2 = User.model2  
> @result3 = User.model3
> 
> Now the various models that belong to User have various different  
> columns/attributes in them, but they do share a common attribute of a  
> time column.
> 
> For use in one of my view I want to, for example, combine @result1,  
> @result2 and @result3 in one main @results hash and order them all by  
> time. Then in my view I can do one simple loop such as:
> 
> for result in @results  
> result.attribute\_from\_model1  
> result.attribute\_from\_model2\_that\_isnt\_in\_model1  
> …  
> end
> 
> I suppose there is a way to do this with either a Ruby (zip?) hash  
> merging method or a single AR call/join. Any thoughts? Thanks.

@result = {}  
[User.model1s, User.model2s, User.model3s].each { | arr | @result.merge  
arr.index\_by(&:time) }

… then… later on back at the farm:  
@result.keys.sort.each {|key| pp @result[key] }

hope this helps…

ilan
