# Habtm - stack too deep error

**URL:** <https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540>\
**Category:** Rails\
**Created:** [May 19, 2007, 4:41am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540 "2007-05-19T04:41:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 19, 2007, 4:41am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/1 "2007-05-19T04:41:49Z")

</div>

Hi all,

I am trying to create many-to-many relationship between models “Topic”  
and “Category”

I am trying following in rails console which gives me exception.

> > t1 = Topic.new(:title =\> “t1”)  
> > c1 = Category.new(:name =\> “c1”)  
> > t1.categories \<\< c1  
> > t1.save  
> > Gives Exception…

Am I doing something really wrong?

Thanks,  
Siddharth

Here is my Topic model  
class Topic \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories,  
:class\_name =\> “Category”,  
:join\_table =\> “topics\_categories”,  
:association\_foreign\_key =\> “category\_id”,  
:foreign\_key =\> “topic\_id”,  
:after\_add =\> :add\_topic\_to\_category,  
:after\_remove =\> :remove\_topic\_from\_category

```
def add_topic_to_category(category)
  category.topics << self unless category.topics.include?(self)
end
def remove_topic_from_category(category)
  category.topics.delete(self) rescue nil
end

```

end

and Category  
class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :topics,  
:class\_name =\> “Topic”,  
:join\_table =\> “topics\_categories”,  
:association\_foreign\_key =\> “topic\_id”,  
:foreign\_key =\> “category\_id”  
end

and topics\_categories table  
create table topics\_categories (  
topic\_id bigint,  
category\_id bigint  
);

Full stack trace  
Loading development environment.

> > t1 = Topic.new(:title =\> “t1”)  
> > =\> #\<Topic:0xb766da88 @new\_record=true,  
> > @attributes={“created\_on”=\>nil, “num\_comments”=\>nil, “title”=\>“t1”,  
> > “updated\_on”=\>nil, “url”=\>“”, “blocked”=\>0, “description”=\>“”,  
> > “user\_id”=\>nil, “num\_views”=\>nil}\>  
> > c1 = Category.new(:name =\> “c1”)  
> > =\> #\<Category:0xb7614cd0 @new\_record=true, @attributes={“name”=\>“c1”,  
> > “blocked”=\>0, “description”=\>“”, “usage\_count”=\>nil}\>  
> > t1.categories \<\< c1  
> > =\> [#\<Category:0xb7614cd0 @topics=[#\<Topic:0xb766da88  
> > @categories=[…], @new\_record=true, @attributes={“created\_on”=\>nil,  
> > “num\_comments”=\>nil, “title”=\>“t1”, “updated\_on”=\>nil, “url”=\>“”,  
> > “blocked”=\>0, “description”=\>“”, “user\_id”=\>nil, “num\_views”=\>nil}\>],  
> > @new\_record=true, @attributes={“name”=\>“c1”, “blocked”=\>0,  
> > “description”=\>“”, “usage\_count”=\>nil}\>]  
> > t1.save  
> > SystemStackError: stack level too deep  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations.rb:912:in  
> > `validate_associated_records_for_categories' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:802:in `run\_validations’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/validations.rb:800:in  
> > `run_validations' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:764:in `valid\_without\_callbacks’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:310:in  
> > `valid?' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:918:in `validate\_associated\_records\_for\_topics’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
> > `method_missing' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/has_and_belongs_to_many_association.rb:81:in `method\_missing’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations.rb:913:in  
> > `validate_associated_records_for_topics' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:802:in `run\_validations’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/validations.rb:800:in  
> > `run_validations' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:764:in `valid\_without\_callbacks’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:310:in  
> > `valid?' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:918:in `validate\_associated\_records\_for\_categories’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
> > `method_missing' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/has_and_belongs_to_many_association.rb:81:in `method\_missing’  
> > … 2796 levels…  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:310:in  
> > `valid?' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:918:in `validate\_associated\_records\_for\_categories’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations/association\_proxy.rb:110:in  
> > `method_missing' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations/has_and_belongs_to_many_association.rb:81:in `method\_missing’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations.rb:913:in  
> > `validate_associated_records_for_categories' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:802:in `run\_validations’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/validations.rb:800:in  
> > `run_validations' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:764:in `valid\_without\_callbacks’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/callbacks.rb:310:in  
> > `valid?' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:735:in `save\_without\_transactions’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:126:in  
> > `save' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:91:in  
> > `transaction' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/transactions.rb:118:in `transaction’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/transactions.rb:126:in  
> > `save’  
> > from (irb):4\>\>

## –

## -|[Siddharth] [S] [Karandikar]|- [www.paahijen.com](http://www.paahijen.com) - Applications in Indian Languages!

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 22, 2007, 1:27am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/2 "2007-05-22T01:27:36Z")

</div>

Siddharth Karandikar wrote:

> I am trying to create many-to-many relationship between models “Topic”  
> and “Category”

Check to ensure that your model file is not being required twice. I have  
had this before when the habtm macro is defined twice by accident  
because the model was being required twice.

Eric

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 22, 2007, 5:13am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/3 "2007-05-22T05:13:51Z")

</div>

On 5/22/07, Eric A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Siddharth Karandikar wrote:
> 
> > I am trying to create many-to-many relationship between models “Topic”  
> > and “Category”
> 
> Check to ensure that your model file is not being required twice. I have  
> had this before when the habtm macro is defined twice by accident  
> because the model was being required twice.

Thanks Eric for reply.

I far as I know, Rails handles all the “requires” part, so how should  
we add a check there?  
Is this rails console specific issue?

- Siddharth

> Eric
> 
> >

## –

-|[Siddharth] [S] [Karandikar]|-  
[www.paahijen.com](http://www.paahijen.com) - Applications in Indian Languages!

## [Siddharth Karandikar | Flickr](http://www.flickr.com/photos/siddharth178/)

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 22, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/4 "2007-05-22T14:56:49Z")

</div>

here is your problem:

from  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations.rb:913:in  
`validate\_associated\_records\_for\_topics’

from  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations.rb:913:in  
`validate\_associated\_records\_for\_categories’

from this i assume you left out the validations from your models in  
your post as this suggests you have validates\_associated :topics in  
your Category model and validates\_associated :categories in your Topic  
model.

this is a big no-no as is causes exactly what you are experiencing, an  
infinite validation loop. when you save your topic it tries to  
validate the associated category which then tries to validate the  
associated topic which then tries to validate the associated category  
and so on.

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 23, 2007, 4:00am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/5 "2007-05-23T04:00:55Z")

</div>

On 5/22/07, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> from this i assume you left out the validations from your models in  
> your post as this suggests you have validates\_associated :topics in  
> your Category model and validates\_associated :categories in your Topic  
> model.
> 
> this is a big no-no as is causes exactly what you are experiencing, an  
> infinite validation loop. when you save your topic it tries to  
> validate the associated category which then tries to validate the  
> associated topic which then tries to validate the associated category  
> and so on.

Thanks Chris for pointing out the real cause. It has to be because of  
infinite loop.  
But if you see the model classes that I have posted in my first mail,  
those doesn’t contain any of these “validates\_associated” methods. So  
rails is trying to do this validations on its own.

Now the question is, how to tell rails not to validate for these things?

- à¤¸à¤¿à¤¦à¥à¤§à¤¾à¤°à¥à¤¥ [Siddharth]  
[written using [www.paahijen.com](http://www.paahijen.com)]

> > > 
> > 
> > - Siddharth  
> > -|[Siddharth] [S] [Karandikar]|-  
> > [www.paahijen.com](http://www.paahijen.com) - Applications in Indian Languages!
> > 
> > ## [Siddharth Karandikar | Flickr](http://www.flickr.com/photos/siddharth178/)
> > 
> > >
> 
> >

## –

-|[Siddharth] [S] [Karandikar]|-  
[www.paahijen.com](http://www.paahijen.com) - Applications in Indian Languages!

## [Siddharth Karandikar | Flickr](http://www.flickr.com/photos/siddharth178/)

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 23, 2007, 4:16am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/6 "2007-05-23T04:16:50Z")

</div>

Shit,

You really over complicated this.

rename your table to categories\_topics and

> class Topic \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :categories,  
> :class\_name =\> “Category”,  
> :join\_table =\> “topics\_categories”,  
> :association\_foreign\_key =\> “category\_id”,  
> :foreign\_key =\> “topic\_id”,  
> :after\_add =\> :add\_topic\_to\_category,  
> :after\_remove =\> :remove\_topic\_from\_category

becomes

class Topic \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories  
end

do the same for Category  
now run your tests

============================================================

the association was over explicit  
rails is based on conformity so you don’t need to configure everything.

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 23, 2007, 5:38am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/7 "2007-05-23T05:38:02Z")

</div>

On 5/23/07, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > :join_table => "topics_categories",
> > :association_foreign_key => "category_id",
> > :foreign_key => "topic_id",
> > :after_add => :add_topic_to_category,
> > :after_remove => :remove_topic_from_category
> > 
> > ```
> 
> becomes
> 
> class Topic \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :categories  
> end

Thanks for showing me the simpler version. But I doubt this will solve  
the “stack too deep” issue.

- Siddharth

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

## –

- à¤¸à¤¿à¤¦à¥à¤§à¤¾à¤°à¥à¤¥ [Siddharth]  
[written using [www.paahijen.com](http://www.paahijen.com)] - Applications in Indian Languages!

## [Siddharth Karandikar | Flickr](http://www.flickr.com/photos/siddharth178/)

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 23, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/8 "2007-05-23T15:49:14Z")

</div>

I agree with Keynan on this, although I probably would have said it  
differently. 🙂

might I suggest that you remove the :after\_add callback from the habtm  
and see what happens? I don’t see a need to have that callback  
because the relationship will exist because you just added it.  
doesn’t matter if you create the relationship via Topic or Category,  
it’s still the same relationship. same goes for your after\_remove  
callback. you’d just be trying to remove the relationship you just  
removed.

So my suggestion is this

Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :topics,  
# this line not necessary if you name join table  
“categories\_topics”  
:join\_table =\> “topics\_categories”,  
:uniq =\> true  
end

Topic \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories,  
# this line not necessary if you name join table  
“categories\_topics”  
:join\_table =\> “topics\_categories”,  
:uniq =\> true  
end

try this out and see what happens

---

<div class="post-metadata">

**Author:** ![Siddharth\_Karandika](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@Siddharth\_Karandika](https://www.ruby-forum.com/u/Siddharth_Karandika)\
**Post date:** [May 24, 2007, 4:09am UTC](https://www.ruby-forum.com/t/habtm-stack-too-deep-error/104540/9 "2007-05-24T04:09:45Z")

</div>

Cool. That works!

Hmm … then I need to see why that’s been done in one of the rails  
recipes chapter. 🙂  
Anyways, thanks guys.

- Siddharth

On 5/23/07, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Topic \< ActiveRecord::Base
> 
> > On 5/23/07, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > > ```
> > > > :join_table => "topics_categories",
> > > > 
> > > > ```
> > 
> > > 
> > 
> > –

## –

- à¤¸à¤¿à¤¦à¥à¤§à¤¾à¤°à¥à¤¥ [Siddharth]  
[written using [www.paahijen.com](http://www.paahijen.com)] - Applications in Indian Languages!

## [Siddharth Karandikar | Flickr](http://www.flickr.com/photos/siddharth178/)
