Nested/not-nested resources

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.

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

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

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

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