Reputation: 107
I have a project with 2 models linked through a habtm relationship and would like to seed the default relationships values as they are fixed and needed for the web aplication to work.
Can't seem to find a way to access the model join table and seed the default values to the object1_id, object2_id as the table is not linked to a model. It's currently being done via SQL directly on postgreSQL.
Any suggestions?
Upvotes: 1
Views: 1801
Reputation: 432
You would need to generate a join table if you have not already done so:
rails g migration CreateJoinTable users roles
And as long as you have habtm in their respective classes:
# app/models/user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
end
Just push the object you are creating onto the associative array:
# db/seeds.rb
bill = User.create(name: "bill")
bill.roles << Role.create(title: "admin")
Upvotes: 3
Reputation: 29349
If you can think of any place where you would want to access the model directly, create the model. Otherwise direct sql query is probably fine.
Upvotes: 0