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

**URL:** <https://www.ruby-forum.com/t/refreshing-a-page-gives-a-stack-level-too-deep-error/131829>\
**Category:** Rails\
**Created:** [March 13, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/refreshing-a-page-gives-a-stack-level-too-deep-error/131829 "2008-03-13T17:18:28Z")\
**Posts on this page:** 4\
**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 13, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/refreshing-a-page-gives-a-stack-level-too-deep-error/131829/1 "2008-03-13T17:18:28Z")

</div>

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: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`resource\_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

---

<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 13, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/refreshing-a-page-gives-a-stack-level-too-deep-error/131829/2 "2008-03-13T17:31:59Z")

</div>

“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

---

<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 13, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/refreshing-a-page-gives-a-stack-level-too-deep-error/131829/3 "2008-03-13T17:47:58Z")

</div>

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…

---

<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 13, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/refreshing-a-page-gives-a-stack-level-too-deep-error/131829/4 "2008-03-13T17:55:53Z")

</div>

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
