# Simple parent child view

**URL:** <https://www.ruby-forum.com/t/simple-parent-child-view/110575>\
**Category:** Rails\
**Created:** [July 27, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/simple-parent-child-view/110575 "2007-07-27T18:12:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![magiclava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@magiclava](https://www.ruby-forum.com/u/magiclava)\
**Post date:** [July 27, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/simple-parent-child-view/110575/1 "2007-07-27T18:12:17Z")

</div>

I have 2 tables (clients and orders). I have create models for them  
both and the relevant belongs\_to has\_many declarations.

I’ve created a controller which has the following code:

def list\_orders  
@all\_clients = Client.find(:all)  
@all\_orders = Order.find(:all)  
end

I’ve created a view which has the following code:  
\<% for client in @all\_clients %\>  
\<%=h(client.name) %\>  
\<% for order in @all\_orders %\>  
\<%=h(order.detail) %\>  
\<% end %\>  
\<% end %\>

Now what i want to see is a list of clients with their individual  
order details listed underneath. Instead of course im seeing each  
client with _every_ client order detail listed underneath. The  
problem I have is that I dont know how to pass the _current_ client id  
to the _order_ loop to get a true parent/child view.

I’m missing something really basic here, but its stumping me at the  
moment.

---

<div class="post-metadata">

**Author:** ![magiclava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@magiclava](https://www.ruby-forum.com/u/magiclava)\
**Post date:** [July 27, 2007, 7:16pm UTC](https://www.ruby-forum.com/t/simple-parent-child-view/110575/2 "2007-07-27T19:16:23Z")

</div>

Here is what I would do. I’m new to this, so perhaps there is a  
better way.

I would take out the Order.find statement.

Instead, take advantage of the methods Rails gives you for free when  
you set up the belongs\_to and has\_many relationship.

For example, you could use the following inside your main loop:

for order in client.orders

instead of

for order in @all\_orders

---

<div class="post-metadata">

**Author:** ![magiclava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@magiclava](https://www.ruby-forum.com/u/magiclava)\
**Post date:** [July 27, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/simple-parent-child-view/110575/3 "2007-07-27T20:29:36Z")

</div>

class Client  
has\_many :orders  
end  
def list\_orders  
@all\_clients = Client.find(:all, :include=\>[:orders])  
end  
for client in all\_clients  
\<%=client.name%\>  
for order in client.orders  
\<%=order.id%\>  
end  
end

---

<div class="post-metadata">

**Author:** ![magiclava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@magiclava](https://www.ruby-forum.com/u/magiclava)\
**Post date:** [July 28, 2007, 12:12am UTC](https://www.ruby-forum.com/t/simple-parent-child-view/110575/4 "2007-07-28T00:12:09Z")

</div>

Fantastic! Thank you both. For those who want a full listing:

TABLES:

DROP TABLE IF EXISTS `demo_development`.`clients`;  
CREATE TABLE `demo_development`.`clients` (  
`id` int(10) unsigned NOT NULL auto\_increment,  
`name` varchar(45) NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `demo_development`.`orders`;  
CREATE TABLE `demo_development`.`orders` (  
`id` int(10) unsigned NOT NULL auto\_increment,  
`client_id` int(10) unsigned NOT NULL,  
`description` varchar(45) NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

\*Create new rails project…

MODELS:  
class Client \< ActiveRecord::Base  
has\_many :orders  
end

class Order \< ActiveRecord::Base  
belongs\_to :clients  
end

CONTROLLER:  
class InvoiceController \< ApplicationController  
def list\_orders  
@all\_clients = Client.find(:all, :include =\> [:orders])  
end  
end

VIEW:(list\_orders.rhtml)  
\<% for client in @all\_clients %\>  
\<%=client.name%\>  
\<% for order in client.orders %\>  
\<%=order.description%\>  
\<% end %\>  
\<% end %\>
