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

**URL:** <https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375>\
**Category:** Ruby\
**Created:** [December 7, 2007, 1:41am UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375 "2007-12-07T01:41:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 7, 2007, 1:41am UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375/1 "2007-12-07T01:41:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 7, 2007, 8:55am UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375/2 "2007-12-07T08:55:24Z")

</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]

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 7, 2007, 12:32pm UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375/3 "2007-12-07T12:32:04Z")

</div>

On Dec 7, 2:55 am, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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` (  
`tag_id` int(11) NOT NULL,  
`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> from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_recor d/base.rb:1863:in `method\_missing’  
> > from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
> > active\_recor  
> > d/associations/association\_collection.rb:141:in `replace' from e:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_recor d/associations.rb:962:in `tags=’  
> > from (irb):37

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 7, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375/4 "2007-12-07T15:46:19Z")

</div>

On 12/7/07, Junkone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> #\<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

```
a.tags << z

```

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 7, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375/5 "2007-12-07T16:55:36Z")

</div>

On Dec 7, 9:45 am, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > omments"=\>nil, “tradedirection”=\>“Short”}\>  
> > –  
> > Rick DeNatale
> 
> My blog on Rubyhttp://talklikeaduck.denhaven2.com/

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

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 7, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375/6 "2007-12-07T18:06:03Z")

</div>

On 12/7/07, Junkone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Dec 7, 9:45 am, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

```
      a.tags.delete(z)

```

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 7, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/help-with-has-and-belongs-to-many/122375/7 "2007-12-07T18:27:11Z")

</div>

On Dec 7, 12:05 pm, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Rick DeNatale
> 
> My blog on Rubyhttp://talklikeaduck.denhaven2.com/

thanks a lot.
