# Combining find results from multiple tables/models

**URL:** <https://www.ruby-forum.com/t/combining-find-results-from-multiple-tables-models/144523>\
**Category:** Rails\
**Created:** [July 30, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/combining-find-results-from-multiple-tables-models/144523 "2008-07-30T16:31:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [July 30, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/combining-find-results-from-multiple-tables-models/144523/1 "2008-07-30T16:31:35Z")

</div>

Hi all,

I’m wondering if it’s possible to merge/combine find results from  
multiple tables. I have a customers model and customers have invoices  
and payments. I need to combine the results from the invoice and  
payment tables to create a customer ledger, which looks like:

* * *

| Date Type Amount Balance

| |
| --- |
| 1/1/07 |
| |

* * *

| 1/2/07 | Invoice | $200.00 | $100.00

| |
| --- |
| 1/5/07 |
| |

* * *

| 1/10/07 | Invoice | $100.00 | $100.00

| |
| --- |

Is there any way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [July 30, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/combining-find-results-from-multiple-tables-models/144523/2 "2008-07-30T16:56:38Z")

</div>

@entries = (@customer.payments + @customer.invoices).sort{|a,b| a.date  
\<=\> b.date}

When iterating through @entries in the view, you’ll can just use  
‘entry.type’ for the second column, if that’s literally all you need.  
It might be nicer, though, to have a different partial for each type,  
and render each accordingly:

\<% @entries.each do |entry| %\>  
\<%= render :partial =\> (@result.type == Payment ? “payment” :  
“invoice”), :locals =\> {:entry =\> entry} %\>  
\<% end %\>

Alternatively, it might be sufficient to simply change the style for  
each line depending on type.

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [July 30, 2008, 4:58pm UTC](https://www.ruby-forum.com/t/combining-find-results-from-multiple-tables-models/144523/3 "2008-07-30T16:58:31Z")

</div>

Max W. wrote:

> @entries = (@customer.payments + @customer.invoices).sort{|a,b| a.date  
> \<=\> b.date}

Oh and by the way, if you need to split this across several pages, you  
can just call .paginate on the results with a page number, ie

@entries = (@customer.payments + @customer.invoices).sort{|a,b| a.date  
\<=\> b.date}.paginate(:page =\> params[:page], :per\_page =\> 20)

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [July 30, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/combining-find-results-from-multiple-tables-models/144523/4 "2008-07-30T17:10:52Z")

</div>

Wow, that’s easy… Kinda figured that rails would make this  
simple… Thanks very much!

On Jul 30, 9:58 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
