Has_and_belongs_to_many associations

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?

On 24 April 2013 09:19, Paul O. [email protected] 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

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 L. wrote in post #1106758:

On 24 April 2013 09:19, Paul O. [email protected] 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

On 24 April 2013 14:09, Paul O. [email protected] 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.

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

In 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