Hi! I have a User model that has_and_belongs_to_many Awards. The awards table is pre-populated, and there''s an awards_users join table. In a rails console, If I do: u = User.find(1) u.awards.build(award_id: 1) the award model that is built is an actual Award model, not an AwardsUser model, like I would expect. so, If i try to save the user model it violates the primary key index because it is also trying to save a new Award, with the id 1. I was expecting this to create a new row in the awards_users table instead, with the user_id and award_id of 1. Any ideas where I''ve gone wrong? -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 24 April 2013 09:19, Paul Ols <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi! > > I have a User model that has_and_belongs_to_many Awards. > The awards table is pre-populated, and there''s an awards_users join > table. > > In a rails console, If I do: > u = User.find(1) > u.awards.build(award_id: 1) > > the award model that is built is an actual Award model, not an > AwardsUser model, like I would expect. > so, If i try to save the user model it violates the primary key index > because it is also trying to save a new Award, with the id 1. > I was expecting this to create a new row in the awards_users table > instead, with the user_id and award_id of 1. > > Any ideas where I''ve gone wrong?You should not try and set the id manually, let Rails take care of that. award = u.awards.build should build an award object, then award.save should save it. Having said that I much prefer to use has_many through and manage the join table myself. I find it easier to follow what is happening. Colin> > -- > 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hmm, perhaps I dont understand the meaning of rails'' has_and_belongs_to_many. I require only the join table to be populated, NOT additional Award objects to be created in the DB. A User should have many rows in awards_users (user_id, award_id). So in terms of a user admin interface, I want to be able to select several (even duplicate) Awards that belong to a particular user (lets say awards with the ID''s 1,2,3,3,4 and 6). The result of saving this user would be rows in awards_users like: award_id | user_id 1 | 1 2 | 1 3 | 1 3 | 1 4 | 1 6 | 1 Thanks. Colin Law wrote in post #1106758:> On 24 April 2013 09:19, Paul Ols <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> the award model that is built is an actual Award model, not an >> AwardsUser model, like I would expect. >> so, If i try to save the user model it violates the primary key index >> because it is also trying to save a new Award, with the id 1. >> I was expecting this to create a new row in the awards_users table >> instead, with the user_id and award_id of 1. >> >> Any ideas where I''ve gone wrong? > > You should not try and set the id manually, let Rails take care of that. > award = u.awards.build > should build an award object, then award.save should save it. > > Having said that I much prefer to use has_many through and manage the > join table myself. I find it easier to follow what is happening. > > Colin-- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 24 April 2013 14:09, Paul Ols <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: Please don''t top post, it makes it difficult to follow the thread. Insert your reply inline at appropriate point(s) in previous message. Thanks.> Hmm, perhaps I dont understand the meaning of rails'' > has_and_belongs_to_many. > > I require only the join table to be populated, NOT additional Award > objects to be created in the DB. > A User should have many rows in awards_users (user_id, award_id). > > So in terms of a user admin interface, I want to be able to select > several (even duplicate) Awards that belong to a particular user (lets > say awards with the ID''s 1,2,3,3,4 and 6). > > The result of saving this user would be rows in awards_users like: > > award_id | user_id > 1 | 1 > 2 | 1 > 3 | 1 > 3 | 1 > 4 | 1 > 6 | 1In that case I am sure it would definitely be easier to use has_many through (see the Rails Guide on ActiveRecord associations) then you will have full control of the join table. Colin> > Thanks. > > Colin Law wrote in post #1106758: >> On 24 April 2013 09:19, Paul Ols <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> the award model that is built is an actual Award model, not an >>> AwardsUser model, like I would expect. >>> so, If i try to save the user model it violates the primary key index >>> because it is also trying to save a new Award, with the id 1. >>> I was expecting this to create a new row in the awards_users table >>> instead, with the user_id and award_id of 1. >>> >>> Any ideas where I''ve gone wrong? >> >> You should not try and set the id manually, let Rails take care of that. >> award = u.awards.build >> should build an award object, then award.save should save it. >> >> Having said that I much prefer to use has_many through and manage the >> join table myself. I find it easier to follow what is happening. >> >> Colin > > -- > 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.