无法批量分配受保护的属性,但使用了attr_accessible

时间:2013-06-24 21:16:52

标签: ruby-on-rails-3 mass-assignment

我收到以下错误:

  

无法批量分配受保护的属性:管理员,学生

当我使用以下 seed.rb 文件运行 rake db:seed 命令时:

roles = [
    Admin: 'Unlimited Access',
    Student: 'Default user role for each user created by sign-in interface.'
]

roles.each do |role, description|
  SecurityUsersRole.find_or_create_by_role_and_description(role,description)
end

我的模型看起来像:

class SecurityUsersRole < ActiveRecord::Base

  # Accessible columns
  attr_accessible :description,
                  :role

  # Relationships
  ...

  # Validations
  validates :description, presence: true, length: { in: 4..32 }, uniqueness: true
  validates :role, length: {  maximum: 256 }, presence: true

end

我已经阅读了很多关于这一点的内容,似乎对于最新的rails版本 Mass Assignment 如果关闭以防止像这样的黑客攻击:

def signup
  params[:user] # => {:name => “ow3ned”, :admin => true}
  @user = User.new(params[:user])
end

关于此问题有很多SO问题,答案总是设置attr_accessible到字段,但我已经这样做了。

编辑:

如果我使用以下语法( {} 而不是 [] ),则会添加第一对语法:

roles = {
    Admin: 'Unlimited Access',
    Student: 'Default user role for each user created by sign-in interface.'
}

如果我这样做,没有任何改变:

SecurityUsersRole.find_or_create_by_role_and_description('Admin','Unlimited Access')
SecurityUsersRole.find_or_create_by_role_and_description('Student','Default user role for each user created by sign-in interface.')

经过一些阅读后,似乎find_or_create_by助手已经depreciated 在Rails 4.0中。我正在使用Rails 3.2,但也决定尝试其他技术:

roles = {
    Admin: 'Unlimited Access',
    Student: 'Default user role for each user created by sign-in interface.',
    Test:'wtf'
}

roles.each do |role, description|
  SecurityUsersRole.where(role:role,description:description).first_or_create
  #puts role
  #puts description
end

上述声明仍然只创建第一条记录。我决定在rails控制台中执行该语句,这是我得到的输出:

  SecurityUsersRole Load (5.0ms)  SELECT security_users_roles.* FROM security_users_roles WHERE security_users_roles.role = 'Admin' AND security_users_roles.description = 'Unlimited Access' FETCH FIRST ROW ONLY
  SecurityUsersRole Load (3.0ms)  SELECT security_users_roles.* FROM security_users_roles WHERE security_users_roles.role = 'Student' AND security_users_roles.description = 'Default user role for each user created by sign-in interface.' FETCH FIRST ROW ONLY
  SecurityUsersRole Exists (1.0ms)  SELECT 1 AS one FROM security_users_roles WHERE security_users_roles.description = 'Default user role for each user created by sign-in interface.' FETCH FIRST ROW ONLY
  SecurityUsersRole Load (1.0ms)  SELECT security_users_roles.* FROM security_users_roles WHERE security_users_roles.role = 'Test' AND security_users_roles.description = 'wtf' FETCH FIRST ROW ONLY
  SecurityUsersRole Exists (1.0ms)  SELECT 1 AS one FROM security_users_roles WHERE security_users_roles.description = 'wtf' FETCH FIRST ROW ONLY
=> {:Admin=>"Unlimited Access", :Student=>"Default user role for each user created by sign-in interface.", :Test=>"wtf"}

我无法理解究竟发生了什么,但是从控制台中选择所有用户只会返回第一个用户。那么,如果搜索其他角色,为什么不创建它们,因为它们不存在?

有人可以建议我可以试试吗?

1 个答案:

答案 0 :(得分:0)

问题是由我的模型验证规则引起的,其中

  # Validations
  validates :description, presence: true, length: { in: 4..32 }, uniqueness: true
  validates :role, length: {  maximum: 256 }, presence: true

应该是

  # Validations
  validates :role, presence: true, length: { in: 4..32 }, uniqueness: true
  validates :description, length: {  maximum: 256 }, presence: true

我更改了:role :description 字段的位置。因此, rake db:seed 命令会插入第一个记录,但第二个记录会出现错误,并且插入将被中止。结果是我总是导入第一个记录。

为了在使用种子时获得验证错误,您应该使用!方法版本,如下所示:

SecurityUsersRole.where(role:role,description:description).first_or_create!