Combining find results from multiple tables/models

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!

@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.

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)

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

On Jul 30, 9:58 am, Max W. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs