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.
On Nov 21, 2007 2:54 PM, [email protected] <
[email protected]> 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
[email protected] 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
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. [email protected]
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.