stack level too deep

Stack Level too deep means there is some function recursing, probably forever. Looking over your code real quick I've seen the :has_many organizations which probably leads to the recursion. When trying to get the parents, Rails will get the Parents of the Parents and so on since it is a property of the Organization so I guess it is not lazy evaluated. Maybe that helps...

Philipp Fehre wrote in post #989443:

Stack Level too deep means there is some function recursing, probably forever. Looking over your code real quick I've seen the :has_many organizations which probably leads to the recursion. When trying to get the parents, Rails will get the Parents of the Parents and so on since it is a property of the Organization so I guess it is not lazy evaluated. Maybe that helps...

Thanks Philipp.. I commented the has_many :organizations sentence but it still keeps showing the errors... The issue in my code comes from this place

   def self.parents

     @organizations = Organization.where("is_company = ?",true)

I guess it keeps asking itself about the companies which are companies within the same table... What do you think?

Leo

yep... and "self" is one of those companies, so it keeps looping...

Add a condition to exclude the current record:

  @organizations = Organization\.where\(\["is\_company = ? AND id <>

?",true, self.id])

(or whatever it takes - I'm not into Rails 3 yet... too much old 2.x stuff to do!)

Ignore me... I see that's a class method, so won't have an instance id...

I assume that the "parents" method returns all the Organizations which have child organisations?

If so, you're using this method instead of a scope? Can't you do something along the lines of the pseudo-SQL below:

SELECT * FROM organisations WHERE organisations.id IN (SELECT parent_id FROM organisations GROUP BY parent_id)

If you can get it working in SQL, you can turn it into a scope.

Michael Pavling wrote in post #989454: