hello folks. please help with the issue: i got something like this: class Product < ActiveRecord::Base has_and_belongs_to_many :groups def nice_method(x) self.groups << Group.find(x) <--------------- HERE! end end so... marked line should associate the models, which, if i''m correct, means: to generate/insert triple (id, product_id, group_id) into table :groups_products . the issue is, that the the generated id is always counted from 1, as if the table :groups_products was empty, even if it isn''t. so i get nice error: Mysql::Error: Duplicate entry ''1'' for key 1: INSERT INTO groups_products (`product_id`, `group_id`, `id`) VALUES (28, 1, 1) you, journeymans and hackers, tell what the hell is wrong? (i am on rails 1.2.3) (i''ve tried also self.group_ids = nice_ids, its the same...) thanks. fero. -- 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 -~----------~----~----~----~------~----~------~--~---
You have a couple of options. 1) In your database (assumed MySQL) you need to alter the groups_products table to have the id field AUTO_INCREMENT: ALTER TABLE groups_products MODIFY COLUMN id INT(11) AUTO_INCREMENT PRIMARY KEY; (Rails migrations actually automatically setup the AUTO_INCREMENT) 2) For the has_and_belongs_to_many you don''t actually need the "id" column. You can do away with that by running: ALTER TABLE groups_products DROP COLUMN id; or in your migration you can use: create_table :groups_products, :id => false do | t | t.column :groups_id, :integer t.column :products_id, :integer end Good day, Chris -- 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 -~----------~----~----~----~------~----~------~--~---
I don''t see why you have the "nice method" below.> def nice_method(x) > self.groups << Group.find(x) <--------------- HERE! > endThe ".groups <<" is the nice method. -- 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 -~----------~----~----~----~------~----~------~--~---
THANKS! the 1) dont work, but 2) is the clear natural solution> The ".groups <<" is the nice method.its created by has_and_belongs_to_many again, thanks. fero. -- 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 -~----------~----~----~----~------~----~------~--~---
On 5/10/07, Fero <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > hello folks. please help with the issue: > > i got something like this: > > class Product < ActiveRecord::Base > > has_and_belongs_to_many :groups > > def nice_method(x) > self.groups << Group.find(x) <--------------- HERE! > end > endThe HABTM has a :uniq option that is supposed to ignore uniques. try class Product < AR::B has_and_belongs_to_many :uniq => true def nice_method(x) self.groups << Group.find(x) end end I think that should take care of the duplicate value for you. HTH Daniel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for pointing that out mahmoud modsaid wrote:> Actually in has_and_belongs_to_many association, the joint table MUST > NOT have an id > > in the migration for creating the join table, you need to skip the > default id creation... check the attribute :id for creating table in > the api > http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M001090 > > for details > http://blog.modsaid.com/2008/09/hasandbelongstomany-duplicate-entry.html > > mahmoud,-- Posted via http://www.ruby-forum.com/.