# Strange behavior in HABTM

**URL:** <https://www.ruby-forum.com/t/strange-behavior-in-habtm/120926>\
**Category:** Rails\
**Created:** [November 21, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/strange-behavior-in-habtm/120926 "2007-11-21T20:55:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 21, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/strange-behavior-in-habtm/120926/1 "2007-11-21T20:55:34Z")

</div>

Hello,

i have here some Problems to add an Item to an has\_and\_belongs\_to\_many  
association. I have a class, that holds Bug entries it is connected to  
an Table that holds some tags. Now ive defined a method which adds a  
tag to the bug. When i do this on a console it works correctly.  
I load an bug entry from db and load an tag item from other table.  
When i add the tag to bug.tags and save the bug is there an new entry  
in the bugs\_tags table. When i do the same in my method it doesnt  
work.

My Method to add some tag:  
def add\_tag  
bug = Bug.find params[:id]  
tag = Tag.find\_by\_name params[:tagname]  
bug.tags \<\< tag  
if tag.save  
flash[:notice] = \_(“Tag successfully added”)  
else  
flash[:notice] = \_(“Tag could not be added”)  
end  
redirect\_to :action =\> “list”  
end

When i try to add an tga i get the following errorpage  
Mysql::Error: Duplicate entry ‘3’ for key 1: INSERT INTO bugs\_tags  
(`tag_id`, `id`, `bug_id`) VALUES (3, 3, 9)

The problem is, that the id gets not incremented from Rails. The first  
entry is correct but when i try to add an second tag i get always this  
error. What could it be?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 21, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/strange-behavior-in-habtm/120926/2 "2007-11-21T21:20:45Z")

</div>

On Nov 21, 2007 2:54 PM, removed\_email\_address@domain.invalid \<  
removed\_email\_address@domain.invalid\> wrote:

> work.  
> end
> 
> Thanks for any help.

Try bug.save instead of tag.save?

Do you have a :unique =\> true on the tag\_id field?

Jason

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 22, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/strange-behavior-in-habtm/120926/3 "2007-11-22T12:59:43Z")

</div>

removed\_email\_address@domain.invalid wrote:

> My Method to add some tag:  
> def add\_tag  
> bug = Bug.find params[:id]  
> tag = Tag.find\_by\_name params[:tagname]  
> bug.tags \<\< tag  
> if tag.save  
> flash[:notice] = \_(“Tag successfully added”)  
> else  
> flash[:notice] = \_(“Tag could not be added”)  
> end  
> redirect\_to :action =\> “list”  
> end

> The problem is, that the id gets not incremented from Rails. The first  
> entry is correct but when i try to add an second tag i get always this  
> error. What could it be?
> 
> Thanks for any help.

Dear bertram,

I am a bit of a n00b, but yesterday I spend on HABTM behaviours in  
Rails. I notice that your join table has an id field. I understand that  
when you use HABTM behaviors, the join table should only contain a pair  
of two foreign keys. In my opinion this is also correct in  
normalization. Perhaps you should delete the id field (and make the  
uniqueness of the pair of two foreign key your primary key).

You can create a join table without id like this:

create\_table :employees\_shifts, :id =\> false do |t|  
t.column :employee\_id, :integer  
t.column :shift\_id, :integer  
end

Also, this line:

bug.tags \<\< tag

Should be in the if statement, so the record will not be added if it  
could not be validated.

I hope you find this useful.

Elinor

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 22, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/strange-behavior-in-habtm/120926/4 "2007-11-22T18:54:15Z")

</div>

Dear Elinor,

thanks for zour help. I tried it and it works now.  
I had no idea how to say rails that it has not to create an id field  
in that table. But i meant it must be something else 😉  
In my Rails book that shows all examples by creating the db tables by  
hand there is also no id field in the table =)

Bertram

On 22 Nov., 12:59, Elinor B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 22, 2007, 9:35am UTC](https://www.ruby-forum.com/t/strange-behavior-in-habtm/120926/5 "2007-11-22T09:35:18Z")

</div>

> > redirect\_to :action =\> “list”  
> > Thanks for any help.
> 
> Try bug.save instead of tag.save?
> 
> Do you have a :unique =\> true on the tag\_id field?
> 
> Jason  
> Hello,

ive tried both ways. The Fields in table are not :unique because its  
an n to m association the only unique field is id which is the primary  
key of this table. But that field is created by the migration itself.
