如何将 JOIN 信息添加到 Rails seeds.rb 文件中?

发布于 2024-11-04 14:40:44 字数 1571 浏览 9 评论 0原文

我正在尝试构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-11-11 14:40:44

假设您在 User 模型中定义了 has_and_belongs_to_many 关联:

has_and_belongs_to_many :roles, :join_table => "roles_users"

在您的 seeds.rb 文件中,您可以向用户添加角色,如下所示(本示例向用户添加所有角色):

u = User.create(:first_name => "admin", :email => "[email protected]", :password => "admin")
Role.all.each { |role| u.roles << role }

仅授予用户“super_admin”角色,你可以这样做:

u = User.create(:first_name => "admin", :email => "[email protected]", :password => "admin")
u.roles << Role.find_by_name("super_admin")

Assuming you have defined a has_and_belongs_to_many association in your User model:

has_and_belongs_to_many :roles, :join_table => "roles_users"

In your seeds.rb file, you can then add roles to a user as shown below (this example adds ALL roles to the user):

u = User.create(:first_name => "admin", :email => "[email protected]", :password => "admin")
Role.all.each { |role| u.roles << role }

To grant the user only the 'super_admin' role, you could do something like this:

u = User.create(:first_name => "admin", :email => "[email protected]", :password => "admin")
u.roles << Role.find_by_name("super_admin")
瘫痪情歌 2024-11-11 14:40:44

通常,我将对象保存到某个变量中:

#Setup our default roles
supr = Role.create(:name => "super_admin")
schl = Role.create(:name => "school_leader")
schs = Role.create(:name => "school_staff")
stut = Role.create(:name => "student")


#Setup and initial super admin user
admin = User.create(:first_name => "admin")
johny = User.create(:first_name => "johny tables")

然后,当我将它们推入关联数组时:

admin.roles << supr << schl << schs
johny.roles << stut

或者您可以在将它们推入关联数组时创建它们,因此不需要传递这么多变量。

Typically I save the objects into some variable:

#Setup our default roles
supr = Role.create(:name => "super_admin")
schl = Role.create(:name => "school_leader")
schs = Role.create(:name => "school_staff")
stut = Role.create(:name => "student")


#Setup and initial super admin user
admin = User.create(:first_name => "admin")
johny = User.create(:first_name => "johny tables")

then as I just push them into the associative array:

admin.roles << supr << schl << schs
johny.roles << stut

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文