bertramseiner-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Nov-21 19:54 UTC
Strange behavior in HABTM
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 21, 2007 2:54 PM, bertramseiner-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org < bertramseiner-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 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. >Try bug.save instead of tag.save? Do you have a :unique => true on the tag_id field? Jason --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bertramseiner-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Nov-22 08:34 UTC
Re: Strange behavior in HABTM
> > 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. > > Try bug.save instead of tag.save? > > Do you have a :unique => true on the tag_id field? > > JasonHello, 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bertramseiner-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bertramseiner-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Nov-22 17:54 UTC
Re: Strange behavior in HABTM
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 Bakker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> bertramsei...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---