Cameron
Cameron

Reputation: 28823

Populate join table using seeds.rb

In my rails app I have three tables: users, roles, and users_roles.

In my seeds.rb file I have the following ruby code:

# create super admin role
Role.create!([{name: "Super Admin", description: "Super Admin (can do anything)"}])
# create user
User.create!([{name: "Cameron", email: "[email protected]", password_digest: "$2a$10$k4FPOZlVmy.hIM5z1scFYeFc8cNcVGpq..MU0529LlSGylEqYLaqC"}])
# make user super admin
User::HABTM_Roles.create!([{user_id: 1, role_id: 1}])

However it fails on that last line: User::HABTM_Roles.create!

How do I add the data into the join table?

Upvotes: 0

Views: 475

Answers (1)

Ursus
Ursus

Reputation: 30056

Try something like

User.first.roles << Role.first

Upvotes: 2

Related Questions