# Nested routes with a 2-way has\_many using join table?

**URL:** <https://www.ruby-forum.com/t/nested-routes-with-a-2-way-has-many-using-join-table/131691>\
**Category:** Rails\
**Created:** [March 12, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/nested-routes-with-a-2-way-has-many-using-join-table/131691 "2008-03-12T15:01:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 12, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/nested-routes-with-a-2-way-has-many-using-join-table/131691/1 "2008-03-12T15:01:35Z")

</div>

I have two classes, InstructionalObject and Assets. They both have\_many  
of the other, implemented through a join table (so it’s like a habtm  
without the habtm).

For the next version of our app, we’re refactoring to RESTful, and i’m  
having trouble with my nested resources/routes. I tried this, in  
routes.rb:

map.resources :assets do |assets|  
assets.resources :instructional\_objects  
end

map.resources :instructional\_objects do |instructional\_objects|  
instructional\_objects.resources :assets  
end

thinking that would let me do

instructional\_objects/:id/assets

to get all the assets belonging to that instructional\_object

and

assets/:id/instructional\_objects

for vice-versa.

However, when i go to these urls i get all assets, and all instructional  
objects respectively: in other words, these two routes seem to be  
equivalent

“instructional\_objects/:id/assets” and “assets”

and these two are equivalent:

“assets/:id/instructional\_objects” and “instructional\_objects”.

To make life more confusing for myself (initially at least) i’m also  
using resource\_this in my controllers. Looking in the log, it looks  
like the right request (i think) is going through:

Processing AssetsController#index (for 127.0.0.1 at 2008-03-12 13:45:56)  
[GET]  
Session ID: 4ca8db0cc675a9dd71fc0ee96031f6ea  
Parameters: {“instructional\_object\_id”=\>“0”, “action”=\>“index”,  
“controller”=\>“assets”}

Can anyone help, please?  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 12, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/nested-routes-with-a-2-way-has-many-using-join-table/131691/2 "2008-03-12T18:18:26Z")

</div>

I understand the need to use has\_many :through in both directions, but  
does your _site_ actually need to present the data to users in both  
directions? Sometimes this can be more of a theoretical need than a  
practical one.

At any rate, if you’re “going RESTful” with brand new controllers, the  
controllers themselves need some help. They don’t know anything about  
the routing that directed the request to them so you’ve got to add  
some code.

The main question that you need to answer is whether you ever need to  
render an index that’s not scoped. That is, will you ever need to  
render a list of assets (all) rather than assets that are associated  
to a particular instructional\_object? If not then you can simply  
modify your index method along these lines:

def index  
@instructional\_object =  
InstructionalObject.find(param[:instructional\_object\_id])  
@assets = @instructional\_object.assets  
end

On Mar 12, 10:01 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [March 12, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/nested-routes-with-a-2-way-has-many-using-join-table/131691/3 "2008-03-12T18:54:27Z")

</div>

> The main question that you need to answer is whether you ever need to  
> render an index that’s not scoped. That is, will you ever need to  
> render a list of assets (all) rather than assets that are associated  
> to a particular instructional\_object? If not then you can simply  
> modify your index method along these lines:
> 
> def index  
> @instructional\_object =  
> InstructionalObject.find(param[:instructional\_object\_id])  
> @assets = @instructional\_object.assets  
> end
> 
> On Mar 12, 10:01 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi Andreas

Funnily enough, after a discussion with my colleague we decided exactly  
that, ie that we never want to see all assets, we always want the ones  
belonging to a specified instructional\_object.

So, i modified the code like you suggest above, but all on one line:

@assets =  
InstructionalObject.find(params[:instructional\_object\_id]).assets

That works. But now i have a new, weird problem: the ‘new\_asset\_path’  
helper on this page needs to be given an instructional object, so it  
knows what it belongs to. So, i modified my code to actually be the  
same as you suggest, ie to define @instructional\_object and @assets.  
And now, when i refresh the page, i get an error saying

“stack level too deep”

and this trace:  
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/attribute\_methods.rb:193:in  
`method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:194:in `method\_missing’  
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/base.rb:1948:in  
`to_param' (eval):2:in `asset\_path’  
app/views/assets/index.html.erb:13:in  
`_run_erb_47app47views47assets47index46html46erb' app/views/assets/index.html.erb:9:in `\_run\_erb\_47app47views47assets47index46html46erb’  
/usr/bin/mongrel\_rails:19:in `load’  
/usr/bin/mongrel\_rails:19

Just from changing the controller to pass @instructional\_object through  
as well! I’m totally mystified here…any suggestions?

thanks  
max
