# Display data from more than one table

**URL:** <https://www.ruby-forum.com/t/display-data-from-more-than-one-table/132136>\
**Category:** Rails\
**Created:** [March 17, 2008, 11:12am UTC](https://www.ruby-forum.com/t/display-data-from-more-than-one-table/132136 "2008-03-17T11:12:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 17, 2008, 11:12am UTC](https://www.ruby-forum.com/t/display-data-from-more-than-one-table/132136/1 "2008-03-17T11:12:30Z")

</div>

Hi guys !

I’m a RoR newbye and need to display the content of more than one table  
in a view. Let’s say I have a Invoices table with a relationship to a  
Customers table. I’d like to display on a single page every invoice  
informations, including the customer name.

How would I achieve this using ActiveRecord ? My Invoice model is only  
able to retrieve data from the Invoices table and my Customer model is  
only able to retrieve data from me Customers table.

Thanks by advance,

Eric.

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 17, 2008, 11:33am UTC](https://www.ruby-forum.com/t/display-data-from-more-than-one-table/132136/2 "2008-03-17T11:33:45Z")

</div>

> if your invoices have a customer\_id, then
> 
> in Invoices model define:  
> belongs\_to :customers
> 
> in Customers model:  
> has\_many :invoices
> 
> then in the controller:  
> @invoices + Invoice.find(:all)
> 
> in the view:  
> \<% @invoices.each do |invoice %\>  
> \<%= invoice.customer.name %\> … plus more invoice data …  
> \<% end %\>
> 
> the relevant point is to define the associations like has\_many &  
> belongs\_to, which gives you this nice syntax of @invoice.customer…

What ? It is THAT simple ?

I say WOW !

Thanks for the help, it works like a charm !

Eric.

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 17, 2008, 11:20am UTC](https://www.ruby-forum.com/t/display-data-from-more-than-one-table/132136/3 "2008-03-17T11:20:42Z")

</div>

Eric Morand wrote:

> Hi guys !
> 
> I’m a RoR newbye and need to display the content of more than one table  
> in a view. Let’s say I have a Invoices table with a relationship to a  
> Customers table. I’d like to display on a single page every invoice  
> informations, including the customer name.
> 
> How would I achieve this using ActiveRecord ? My Invoice model is only  
> able to retrieve data from the Invoices table and my Customer model is  
> only able to retrieve data from me Customers table.
> 
> Thanks by advance,
> 
> Eric.

if your invoices have a customer\_id, then

in Invoices model define:  
belongs\_to :customers

in Customers model:  
has\_many :invoices

then in the controller:  
@invoices + Invoice.find(:all)

in the view:  
\<% @invoices.each do |invoice %\>  
\<%= invoice.customer.name %\> … plus more invoice data …  
\<% end %\>

the relevant point is to define the associations like has\_many &  
belongs\_to, which gives you this nice syntax of @invoice.customer…

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 17, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/display-data-from-more-than-one-table/132136/4 "2008-03-17T17:30:30Z")

</div>

AndyV wrote:

> On Mar 17, 6:20 am, Thorsten M. \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > then in the controller:  
> > @invoices + Invoice.find(:all)
> 
> In the situation described you may want to improve the query:
> 
> @customer = Customer.find(params[:customer\_id], :include=\>:invoices)  
> @invoices = @customer.invoices
> 
> That will save you a db roundtrip, often the most taxing part of the  
> app.

Thanks, that’s even better !

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [March 17, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/display-data-from-more-than-one-table/132136/5 "2008-03-17T14:06:23Z")

</div>

On Mar 17, 6:20 am, Thorsten M. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> then in the controller:  
> @invoices + Invoice.find(:all)

In the situation described you may want to improve the query:

@customer = Customer.find(params[:customer\_id], :include=\>:invoices)  
@invoices = @customer.invoices

That will save you a db roundtrip, often the most taxing part of the  
app.
