I have a model Asset which has_and_belongs_to_many :tags. Tag has only one field, name. There is a table assets_tags which only has an asset_id and a tag_id. I can add a tag to one asset. If I try to add the same tag to another asset though (asset.tags << tag) I get this error: Mysql::Error: #23000Duplicate entry ''4'' for key 1: INSERT INTO assets_tags (`tag_id`, `id`, `asset_id`) VALUES (4, 4, 1)←[0m Any ideas what could be causing that? Why does that table have 3 values (where does ''id'' come from) when it''s just a join table? Why does the tag''s id get assigned to this strange ''id'' column''s value? Seriously, I add it like this... asset.tags << tag asset.save -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
OK, I guess the join tables always have an id added. But the problem remains. -- 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 -~----------~----~----~----~------~----~------~--~---
Pretty sure the id is your problem, remove it and I bet all will be well. you can do this in your migrations: create_table :my_table_name, :id => false do |t| ... ... end to avoid the id on join tables in the future good luck! Tim On Aug 30, 1:01 pm, Dave Smith <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> OK, I guess the join tables always have an id added. But the problem > remains. > -- > 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 -~----------~----~----~----~------~----~------~--~---
Cool, I''ll give it a try. Why is the id there in the first place? Does rails automatically create a primary key for any create_table call? -- 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 -~----------~----~----~----~------~----~------~--~---