如何将 JOIN 信息添加到 Rails seeds.rb 文件中?
我正在尝试构建一个 seeds.rb 文件以将初始管理员用户添加到数据库中。我有一个用户表和模型,以及一个角色表和模型。我有一个联接表,roles_users 来联接用户角色和权限。这是架构:
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "roles_users", :id => false, :force => true do |t|
t.integer "role_id"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "last_name"
end
我已经弄清楚如何使用各自的模型添加用户和角色:
#Setup our default roles
Role.create(:name => "super_admin")
Role.create(:name => "school_leader")
Role.create(:name => "school_staff")
Role.create(:name => "student")
#Setup and initial super admin user
User.create(:first_name => "admin", :email => "[email protected]", :password => "admin")
如何添加联接以授予管理员 super_admin 权限(使用的数据库是 sqlite3)?
I'm trying to build a seeds.rb file to add an initial admin user to the database. I have a Users table and model, and a Roles table and model. I have a join table, roles_users to join the users role and permissions. Here's the schema:
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "roles_users", :id => false, :force => true do |t|
t.integer "role_id"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "last_name"
end
I've figured out how to add the Users and the Roles using the repsective models for each:
#Setup our default roles
Role.create(:name => "super_admin")
Role.create(:name => "school_leader")
Role.create(:name => "school_staff")
Role.create(:name => "student")
#Setup and initial super admin user
User.create(:first_name => "admin", :email => "[email protected]", :password => "admin")
How do I add the join to make grant the admin super_admin privileges (database being used is sqlite3)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您在 User 模型中定义了 has_and_belongs_to_many 关联:
在您的 seeds.rb 文件中,您可以向用户添加角色,如下所示(本示例向用户添加所有角色):
仅授予用户“super_admin”角色,你可以这样做:
Assuming you have defined a has_and_belongs_to_many association in your User model:
In your seeds.rb file, you can then add roles to a user as shown below (this example adds ALL roles to the user):
To grant the user only the 'super_admin' role, you could do something like this:
通常,我将对象保存到某个变量中:
然后,当我将它们推入关联数组时:
或者您可以在将它们推入关联数组时创建它们,因此不需要传递这么多变量。
Typically I save the objects into some variable:
then as I just push them into the associative array:
Or you can just create them when you are pushing them into the associative array, so there would be no need to pass so many variables around.