# Help with has\_and\_belongs\_to\_many

**URL:** <https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816>\
**Category:** ruby-talk\
**Created:** [7 December 2007 00:39 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816 "2007-12-07T00:39:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)\
**Post date:** [7 December 2007 00:39 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816/1 "2007-12-07T00:39:59Z")

</div>

class Trade \< ActiveRecord::Base

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :tags

class Tag \< ActiveRecord::Base  
&nbsp;&nbsp;&nbsp;&nbsp;set\_table\_name "tags"  
&nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :trades

end

how to i add records to the table tags\_trades using active rcord. i  
dont have a model for tags\_trades becaues the docs states that i  
dont need one.

thanks in advance.

---

<div class="post-metadata">

**Author:** ![Peter\_Bunyan](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@Peter\_Bunyan](https://rubytalk.org/u/Peter_Bunyan)\
**Post date:** [7 December 2007 07:55 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816/2 "2007-12-07T07:55:24Z")

</div>

Junkone wrote:

> class Trade \< ActiveRecord::Base
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :tags
> 
> class Tag \< ActiveRecord::Base  
> &nbsp;&nbsp;&nbsp;&nbsp;set\_table\_name "tags"  
> &nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :trades
> 
> end
> 
> how to i add records to the table tags\_trades using active rcord. i  
> dont have a model for tags\_trades becaues the docs states that i  
> dont need one.
> 
> thanks in advance.

Well, first off, you forgot to end the Trade class. And you forgot to  
give it a table name.

Once you've done that, and you've created your instance of the Trade  
class (let's call it trade, just to be simple...) and a bunch of Tag  
classes with silly names:

trade.tags = [cheese, timeshare, lubricant]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)\
**Post date:** [7 December 2007 11:30 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816/3 "2007-12-07T11:30:08Z")

</div>

> Junkone wrote:  
> \> class Trade \< ActiveRecord::Base
> 
> \> has\_and\_belongs\_to\_many :tags
> 
> \> class Tag \< ActiveRecord::Base  
> \> set\_table\_name "tags"  
> \> has\_and\_belongs\_to\_many :trades
> 
> \> end
> 
> \> how to i add records to the table tags\_trades using active rcord. i  
> \> dont have a model for tags\_trades becaues the docs states that i  
> \> dont need one.
> 
> \> thanks in advance.
> 
> Well, first off, you forgot to end the Trade class. And you forgot to  
> give it a table name.
> 
> Once you've done that, and you've created your instance of the Trade  
> class (let's call it trade, just to be simple...) and a bunch of Tag  
> classes with silly names:
> 
> trade.tags = [cheese, timeshare, lubricant]  
> --  
> Posted viahttp://www.ruby-forum.com/.- Hide quoted text -
> 
> - Show quoted text -

i tried that but does not work.  
i do have the table defined  
CREATE TABLE `tags_trades` (  
&nbsp;&nbsp;`tag_id` int(11) NOT NULL,  
&nbsp;&nbsp;`trade_id` int(11) NOT NULL  
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

i get error when assigning many to many relationship

> > a=Trade.find(1)

=\> #\<Trade:0x477f4bc @attributes={"tradestatus"=\>"O", "size"=\>nil,  
"mfe"=\>nil, "  
tradestrategy"=\>nil, "symbol\_alias"=\>"PRGX", "tradeinterval"=\>"DAILY",  
"getpictu  
res"=\>nil, "tradename"=\>nil, "id"=\>"1", "mae"=\>nil,  
"grossrevenue"=\>nil, "tradec  
omments"=\>nil, "tradedirection"=\>"Short"}\>

> > z=Tag.find(1)

=\> #\<Tag:0x4779044 @attributes={"id"=\>"1", "description"=\>"exampe1"}\>

> > a.tags=z

NoMethodError: undefined method `each' for #\<Tag:0x4779044\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_recor  
d/base.rb:1863:in `method\_missing'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_recor  
d/associations/association\_collection.rb:141:in `replace'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_recor  
d/associations.rb:962:in `tags='  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):37

> **···**
>
> On Dec 7, 2:55 am, Peter Bunyan \<peter.bun...@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [7 December 2007 14:45 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816/4 "2007-12-07T14:45:38Z")

</div>

You don't replace a habtm relationship with an assignment, you add to it

&nbsp;&nbsp;&nbsp;&nbsp;a.tags \<\< z

> **···**
>
> On 12/7/07, Junkone \<junkone1@gmail.com\> wrote:
> 
> > i get error when assigning many to many relationship  
> > \>\> a=Trade.find(1)  
> > =\> #\<Trade:0x477f4bc @attributes={"tradestatus"=\>"O", "size"=\>nil,  
> > "mfe"=\>nil, "  
> > tradestrategy"=\>nil, "symbol\_alias"=\>"PRGX", "tradeinterval"=\>"DAILY",  
> > "getpictu  
> > res"=\>nil, "tradename"=\>nil, "id"=\>"1", "mae"=\>nil,  
> > "grossrevenue"=\>nil, "tradec  
> > omments"=\>nil, "tradedirection"=\>"Short"}\>  
> > \>\> z=Tag.find(1)  
> > =\> #\<Tag:0x4779044 @attributes={"id"=\>"1", "description"=\>"exampe1"}\>  
> > \>\> a.tags=z  
> > NoMethodError: undefined method `each' for #\<Tag:0x4779044\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)\
**Post date:** [7 December 2007 15:54 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816/5 "2007-12-07T15:54:59Z")

</div>

thanks. how do you do the reverse. if i want to remove an existing  
relationship.

> **···**
>
> On Dec 7, 9:45 am, Rick DeNatale \<rick.denat...@gmail.com\> wrote:
> 
> > On 12/7/07, Junkone \<junko...@gmail.com\> wrote:
> > 
> > \> i get error when assigning many to many relationship  
> > \> \>\> a=Trade.find(1)  
> > \> =\> #\<Trade:0x477f4bc @attributes={"tradestatus"=\>"O", "size"=\>nil,  
> > \> "mfe"=\>nil, "  
> > \> tradestrategy"=\>nil, "symbol\_alias"=\>"PRGX", "tradeinterval"=\>"DAILY",  
> > \> "getpictu  
> > \> res"=\>nil, "tradename"=\>nil, "id"=\>"1", "mae"=\>nil,  
> > \> "grossrevenue"=\>nil, "tradec  
> > \> omments"=\>nil, "tradedirection"=\>"Short"}\>  
> > \> \>\> z=Tag.find(1)  
> > \> =\> #\<Tag:0x4779044 @attributes={"id"=\>"1", "description"=\>"exampe1"}\>  
> > \> \>\> a.tags=z  
> > \> NoMethodError: undefined method `each' for #\<Tag:0x4779044\>  
> > \> from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/
> > 
> > You don't replace a habtm relationship with an assignment, you add to it
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;a.tags \<\< z
> > 
> > --  
> > Rick DeNatale
> > 
> > My blog on Rubyhttp://talklikeaduck.denhaven2.com/

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [7 December 2007 17:05 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816/6 "2007-12-07T17:05:04Z")

</div>

a.tags.delete(z)

> **···**
>
> On 12/7/07, Junkone \<junkone1@gmail.com\> wrote:
> 
> > On Dec 7, 9:45 am, Rick DeNatale \<rick.denat...@gmail.com\> wrote:
> 
> > \> You don't replace a habtm relationship with an assignment, you add to it  
> > \>  
> > \> a.tags \<\< z
> 
> > thanks. how do you do the reverse. if i want to remove an existing  
> > relationship.
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)\
**Post date:** [7 December 2007 17:25 UTC](https://rubytalk.org/t/help-with-has-and-belongs-to-many/42816/7 "2007-12-07T17:25:05Z")

</div>

thanks a lot.

> **···**
>
> On Dec 7, 12:05 pm, Rick DeNatale \<rick.denat...@gmail.com\> wrote:
> 
> > On 12/7/07, Junkone \<junko...@gmail.com\> wrote:
> > 
> > \> On Dec 7, 9:45 am, Rick DeNatale \<rick.denat...@gmail.com\> wrote:  
> > \> \> You don't replace a habtm relationship with an assignment, you add to it
> > 
> > \> \> a.tags \<\< z  
> > \> thanks. how do you do the reverse. if i want to remove an existing  
> > \> relationship.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.tags.delete(z)
> > 
> > --  
> > Rick DeNatale
> > 
> > My blog on Rubyhttp://talklikeaduck.denhaven2.com/
