# Newbie question about has\_and\_belongs\_to\_many and destroy

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-has-and-belongs-to-many-and-destroy/126033>\
**Category:** Rails\
**Created:** [January 14, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/newbie-question-about-has-and-belongs-to-many-and-destroy/126033 "2008-01-14T22:05:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [January 14, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/newbie-question-about-has-and-belongs-to-many-and-destroy/126033/1 "2008-01-14T22:05:08Z")

</div>

Hi all.

I think this must be a newbie moment, I really hope someone can help  
me out! 🙂

I have a has\_and\_belongs\_to\_many relationship set up, all of which is  
working except for destroy! When I try to delete a company record with  
“Company.delete(1)” I get an error (below) from rails.

I have a “Company” table, and a “Category” table, and a join table  
between the 2 called categories\_companies.

My company model looks like this :-

class Company \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :Category  
end

and my Category model :-

class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :Company  
acts\_as\_tree :order =\> “name”  
end

And finally, the error from script/console :-

> > Company.destroy(1)  
> > NoMethodError: undefined method `clear' for Category:Class from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1238:in`method\_missing’  
> > from (eval):3:in `destroy_without_callbacks' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/callbacks.rb:321:in`destroy\_without\_transactions’  
> > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
> > active\_record/transactions.rb:125:in `destroy' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/connection_adapters/abstract/database_statements.rb: 59:in`transaction’  
> > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
> > active\_record/transactions.rb:95:in `transaction' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/transactions.rb:121:in`transaction’  
> > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
> > active\_record/transactions.rb:125:in `destroy' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:488:in`destroy’  
> > from (irb):1

Thanks all!

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [January 15, 2008, 9:56am UTC](https://www.ruby-forum.com/t/newbie-question-about-has-and-belongs-to-many-and-destroy/126033/2 "2008-01-15T09:56:04Z")

</div>

Anyone?

On Jan 14, 9:04 pm, Matt R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [January 15, 2008, 11:25am UTC](https://www.ruby-forum.com/t/newbie-question-about-has-and-belongs-to-many-and-destroy/126033/3 "2008-01-15T11:25:06Z")

</div>

Matt R. wrote:

> Anyone?
> 
> On Jan 14, 9:04 pm, Matt R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Does it work if you do

Company.find(1).destroy

I’ve not tested this, but i’m thinking that Company.delete(1) might be a  
class method, which might not make the callbacks work (it does seem to  
be saying something about callbacks in the errors). Whereas the above  
would be an instance method.

Bit of a guess but worth a try…?

---

<div class="post-metadata">

**Author:** ![hazmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hazmatt](https://www.ruby-forum.com/u/hazmatt)\
**Post date:** [January 15, 2008, 11:40am UTC](https://www.ruby-forum.com/t/newbie-question-about-has-and-belongs-to-many-and-destroy/126033/4 "2008-01-15T11:40:08Z")

</div>

On 15 Jan 2008, at 08:54, Matt R. wrote:

> Anyone?

Not sure that it matters but as a general rule you should write  
habtm :categories not habtm :Category (and similarly hatbm :companies)

Fred
