# Nested/not-nested resources

**URL:** <https://www.ruby-forum.com/t/nested-not-nested-resources/122992>\
**Category:** Rails\
**Created:** [December 13, 2007, 9:17am UTC](https://www.ruby-forum.com/t/nested-not-nested-resources/122992 "2007-12-13T09:17:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imobach\_GonzSSSSlez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bbe5ce/32.png) [@Imobach\_GonzSSSSlez](https://www.ruby-forum.com/u/Imobach_GonzSSSSlez)\
**Post date:** [December 13, 2007, 9:17am UTC](https://www.ruby-forum.com/t/nested-not-nested-resources/122992/1 "2007-12-13T09:17:09Z")

</div>

Hi all,

Suppose I’ve got two resources: “customers” and “bills”. Each “bill”  
belongs to a “customer”. It seems clear to me that the routes.rb must  
contain something like this:

map.resources :customers do |customer|  
customer.resources :bills  
end

So, I could get all the bills for a customer with this URI: ‘/  
customers/3/bills’

But what if I wanna get ALL the bills (for all customers) using  
something like: ‘/bills’?

I guess I’ve got two options:

a) Add “map.resources :bills” to the routes.rb file and, in  
BillsController, check for a customer\_id parameter (if it’s not set,  
I’d list all the bills).  
b) Add a new controller “all\_bills” that I could use only for listing  
ALL the bills (and maybe adding, editing or destroying bills out of  
the customer context).

What’s the “Rails-way”? Any other idea?

Thank you very much in advance.

---

<div class="post-metadata">

**Author:** ![Imobach\_GonzSSSSlez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bbe5ce/32.png) [@Imobach\_GonzSSSSlez](https://www.ruby-forum.com/u/Imobach_GonzSSSSlez)\
**Post date:** [December 15, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/nested-not-nested-resources/122992/2 "2007-12-15T19:56:52Z")

</div>

Thank you. I suppose that it’s the logical way.

---

<div class="post-metadata">

**Author:** ![Imobach\_GonzSSSSlez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bbe5ce/32.png) [@Imobach\_GonzSSSSlez](https://www.ruby-forum.com/u/Imobach_GonzSSSSlez)\
**Post date:** [December 13, 2007, 11:15am UTC](https://www.ruby-forum.com/t/nested-not-nested-resources/122992/3 "2007-12-13T11:15:07Z")

</div>

both your solutions are ok, i’ve used variations of them in the past,  
depending mainly on the question how much the index/customer and  
index/all has different view and controller code. in most cases i would  
go for the first variant if i can keep it simple enough

---

<div class="post-metadata">

**Author:** ![Imobach\_GonzSSSSlez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bbe5ce/32.png) [@Imobach\_GonzSSSSlez](https://www.ruby-forum.com/u/Imobach_GonzSSSSlez)\
**Post date:** [December 16, 2007, 1:27am UTC](https://www.ruby-forum.com/t/nested-not-nested-resources/122992/4 "2007-12-16T01:27:10Z")

</div>

You could use named route as well. Declaring map.resources :bills gives  
you  
too many methods that you don’t need.
