# :has\_many and :controller specified in routes.rb

**URL:** <https://www.ruby-forum.com/t/has-many-and-controller-specified-in-routes-rb/185627>\
**Category:** Rails\
**Created:** [March 23, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/has-many-and-controller-specified-in-routes-rb/185627 "2010-03-23T18:51:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gsw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@gsw](https://www.ruby-forum.com/u/gsw)\
**Post date:** [March 23, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/has-many-and-controller-specified-in-routes-rb/185627/1 "2010-03-23T18:51:36Z")

</div>

Have an app using older version of Rails (2.3.2) that I need some  
routing assistance with if anyone has a minute.

The app was originally designed to be purely html view, and an xml and  
json api was hacked on. To keep it clean, we are now moving the first  
version of the api (v1) under its own directory (v1) under app/  
controllers but still responding to the old paths.

One of the parts of the new routes.rb specifies:  
map.resources :buckets, :has\_many =\> :apples, :controller =\> ‘v1/  
buckets’

However, when I do rake routes, this produces:  
…  
buckets GET /buckets(.:format) {:controller=\>“v1/  
buckets”, :action=\>“index”}  
POST /buckets(.:format) {:controller=\>“v1/  
buckets”, :action=\>“create”}  
new\_bucket GET /buckets/new(.:format) {:controller=\>“v1/  
buckets”, :action=\>“new”}  
edit\_bucket GET /buckets/:id/edit(.:format) {:controller=\>“v1/  
buckets”, :action=\>“edit”}  
bucket GET /buckets/:id(.:format) {:controller=\>“v1/  
buckets”, :action=\>“show”}  
PUT /buckets/:id(.:format) {:controller=\>“v1/  
buckets”, :action=\>“update”}  
DELETE /buckets/:id(.:format) {:controller=\>“v1/  
buckets”, :action=\>“destroy”}  
bucket\_apples GET /buckets/:bucket\_id/apples(.:format)  
{:controller=\>“apples”, :action=\>“index”}  
POST /buckets/:bucket\_id/apples(.:format)  
{:controller=\>“apples”, :action=\>“create”}  
new\_bucket\_apple GET /buckets/:bucket\_id/apples/new(.:format)  
{:controller=\>“apples”, :action=\>“new”}  
edit\_bucket\_apple GET /buckets/:bucket\_id/apples/:id/edit(.:format)  
{:controller=\>“apples”, :action=\>“edit”}  
bucket\_apple GET /buckets/:bucket\_id/apples/:id(.:format)  
{:controller=\>“apples”, :action=\>“show”}  
PUT /buckets/:bucket\_id/apples/:id(.:format)  
{:controller=\>“apples”, :action=\>“update”}  
DELETE /buckets/:bucket\_id/apples/:id(.:format)  
{:controller=\>“apples”, :action=\>“destroy”}  
…  
(sorry for the bad formatting)

Notice that the routes for the actions specific to the model  
referenced by :has\_many ignores the specification of the controller  
(they go to :controller=\>“apples” instead of :controller=\>“v1/  
apples”).

Sorry for the n00b routing question. I didn’t find such an example of  
how to set the controller of the :has\_many referenced model yet, but  
maybe it is an option somewhere, or I could setup another route for  
that?

---

<div class="post-metadata">

**Author:** ![gsw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@gsw](https://www.ruby-forum.com/u/gsw)\
**Post date:** [March 23, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/has-many-and-controller-specified-in-routes-rb/185627/2 "2010-03-23T21:46:02Z")

</div>

Maybe you could use:

map.resources :buckets, :controller =\> ‘v1/buckets’ do |buckets|  
buckets.resources :apples, :controller =\> ‘v1/apples’  
end

I know this is a bit more code than just using :has\_many, but I don’t  
think  
:has\_many lets you specify a different controller.

/Lasse

2010/3/23 gsw [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
