Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何将连接信息添加到rails seeds.rb文件中?_Ruby On Rails_Ruby_Ruby On Rails 3_Join_Sqlite3 Ruby - Fatal编程技术网

Ruby on rails 如何将连接信息添加到rails seeds.rb文件中?

Ruby on rails 如何将连接信息添加到rails seeds.rb文件中?,ruby-on-rails,ruby,ruby-on-rails-3,join,sqlite3-ruby,Ruby On Rails,Ruby,Ruby On Rails 3,Join,Sqlite3 Ruby,我正在尝试构建一个seeds.rb文件,以便向数据库中添加一个初始管理员用户。我有一个用户表和模型,还有一个角色表和模型。我有一个加入表,角色\用户加入用户的角色和权限。以下是模式: create_table "roles", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "roles_us

我正在尝试构建一个seeds.rb文件,以便向数据库中添加一个初始管理员用户。我有一个用户表和模型,还有一个角色表和模型。我有一个加入表,角色\用户加入用户的角色和权限。以下是模式:

  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
我已经找到了如何使用repsective模型添加用户和角色的方法:

#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 => "admin@domain.com", :password => "admin")

如何添加联接以授予管理员超级管理员权限(使用的数据库是sqlite3)?

假设您已在用户模型中定义了一个has\u和属于多个关联:

has_and_belongs_to_many :roles, :join_table => "roles_users"
在seeds.rb文件中,您可以向用户添加角色,如下所示(此示例向用户添加所有角色):

u=User.create(:first\u name=>“admin”,:email=>)admin@domain.com“,:密码=>“管理员”)
Role.all.each{| Role | u.roles“admin”,:email=>“admin@domain.com“,:密码=>“管理员”)

u、 角色通常我会将对象保存到某个变量中:

#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
管理员角色
#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