Refreshing a page gives a "stack level too deep" error

I’m converting our app into restful rails. A couple of my classes
(Resource and Asset) have a has_many relationship with each other -
here’s the routes:

map.resources :assets, :has_many=>[:resources]
map.resources :resources, :has_many=>[:assets]

I’m getting something weird in my view pages. When i go to

/resources/4/assets

it works. I go to the page again (just hitting enter in the address
bar) and i get the error below. I restart mongrel and it works again.
Refresh again and it breaks again: every time. I can go to a different
url (eg /resources/4) and its fine: come back and its broken again. My
colleague, running the same code on his mac (i’m using ubuntu linux)
doesn’t get the error.

Has anyone seen anything like this before? I’ve only got it since we
started using restful rails. I’ve tried rebooting. I’ve tried running
webrick instead of mongrel. No joy…

Here’s the trace: it seems like it has an issue with my nested path
helper, “resource_asset_path”.

Showing assets/index.html.erb where line #13 raised:

stack level too deep

Extracted source (around line #13):

10:


11: <%=h asset.name %>
12: <%=h asset.description %>
13: <%= link_to ‘Show’, resource_asset_path(@resource, asset)
%>
14: <%= link_to ‘Edit’, edit_asset_path(asset) %>
15: <%= link_to ‘Destroy’, resource_asset_path(@resource,
asset), :confirm => ‘Are you sure?’, :method => :delete %>
16:

RAILS_ROOT: /home/jars/rails/lesson_planner/branches/bundles
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:186:in
method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:194:inmethod_missing’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1948:in
to_param' (eval):2:inresource_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

“stack level to deep” sounds like something running into an infinite
recursion.
maybe those has_many relations in the routes or you redirect the action
to itself…
first thing i would try is remove those has_many from the routes

sorry, just some ideas without realy knowing what’s going on

Thorsten M. wrote:

“stack level to deep” sounds like something running into an infinite
recursion.
maybe those has_many relations in the routes or you redirect the action
to itself…
first thing i would try is remove those has_many from the routes

sorry, just some ideas without realy knowing what’s going on

That’s what i thought, i’ve seen it before when writing recursive
methods and not bottoming out properly. The weirdness is

a) why does it work the first time and crash when i refresh
b) why does it work for someone else and not me when he has the same
revision of our project?

If it take out the has_many, and just have normal routes, then the urls
don’t work: i can manually put them in like so:

map.resources :assets
map.resources :resources

map.connect ‘resources’, :controller => ‘resources’
map.connect ‘resources/:id’, :controller => ‘resources’
map.connect ‘resources/:resource_id/assets’, :controller => ‘assets’
map.connect ‘resources/:resource_id/assets/:id’, :controller =>
‘assets’

But then the helpers don’t work…i’d rather have working nested
resources tbh. Thanks for looking though…

Max W. wrote:

Thorsten M. wrote:

“stack level to deep” sounds like something running into an infinite
recursion.
maybe those has_many relations in the routes or you redirect the action
to itself…
first thing i would try is remove those has_many from the routes

sorry, just some ideas without realy knowing what’s going on

That’s what i thought, i’ve seen it before when writing recursive
methods and not bottoming out properly. The weirdness is

a) why does it work the first time and crash when i refresh
b) why does it work for someone else and not me when he has the same
revision of our project?

If it take out the has_many, and just have normal routes, then the urls
don’t work: i can manually put them in like so:

map.resources :assets
map.resources :resources

map.connect ‘resources’, :controller => ‘resources’
map.connect ‘resources/:id’, :controller => ‘resources’
map.connect ‘resources/:resource_id/assets’, :controller => ‘assets’
map.connect ‘resources/:resource_id/assets/:id’, :controller =>
‘assets’

But then the helpers don’t work…i’d rather have working nested
resources tbh. Thanks for looking though…

I take it back, i took out this line and it’s fine:
map.resources :assets, has_many => [:resources]

This is ok for us as we never need to do a nested
assets/:asset_id/resources call in the url.

What made me put it in originally was the fact that an asset :has_many
=> resources, but that can happily live in the model, and i can still
say things like
@asset.resources in our code.

I guess the lesson is that routes should be there for a reason, rather
than blindly mirroring the model associations.

cheers!
max