“警告:不能批量分配受保护的属性" [英] "WARNING: Can't mass-assign protected attributes"

查看:16
本文介绍了“警告:不能批量分配受保护的属性"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 RESTful 技术来生成模型(实际上,我使用的是 Devise gem,它为我完成了这项工作),并且我向模型添加了名为 first_name 和 last_name 的新字段.迁移很顺利.我在模型中添加了 attr_accessor :first_name, :last_name 并希望它可以正常工作.但是当我尝试使用 Doctor.create({:first_name=>"MyName"}) 等批量分配新实例时,我收到错误消息,说我无法批量分配受保护的属性.

I have used RESTful techniques to generate a model (in fact, I am using Devise gem, which does that for me), and I have added new fields called first_name and last_name to the model. Migration went fine. I added attr_accessor :first_name, :last_name to the model and expected it would just work. But when I try to mass-assign new instances with Doctor.create({:first_name=>"MyName"}) etc., I am getting errors saying I can't mass-assign protected attributes.

我认为使用 attr_accessor 的全部目的是绕过模型字段的保护.你能帮我理解这条消息吗?

I thought the whole point of using attr_accessor was to get around the protectedness of the fields of a model. Can you help me make sense of this message?

哦,顺便说一下,记录也没有被创建.我认为它们应该是,因为这只是一个警告,但它们不在数据库中.

oh, and by the way the records do not get created either. I thought they should be since this is just a warning, but they are not on the database.

Edit2:这是我的模型

here is my model

class Doctor < User
  has_many :patients
  has_many :prescriptions, :through=> :patients

  validates_presence_of :invitations, :on => :create, :message => "can't be blank"

  attr_accessor :invitations
end

和模式,它没有 first_name 和 last_name,因为它们是在 users 表中创建的,这是医生的祖先.我使用了单表继承.

and the schema, which doesn't have the first_name and last_name because they are created in the users table, which is the ancestor of doctors. I used single table inheritance.

create_table :doctors do |t|
  t.integer :invitations

  t.timestamps
end

这是改变用户表的迁移

add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :type, :string

这是种子文件.我不包括 truncate_db_table 方法,但它有效.

here is the seed file. I am not including the truncate_db_table method, but it works.

%w{doctors patients}.each do |m|
  truncate_db_table(m)  
end  

Doctor.create(:invitations=>5, :email=>"email@gmail.com", :first_name=>"Name", :last_name=>"LastName")
Patient.create(:doctor_id=>1, :gender=>"male", :date_of_birth=>"1991-02-24")

推荐答案

不要将 attr_accessorattr_accessible 混淆.访问器内置于 Ruby 中并定义了一个 getter 方法 - model_instance.foo #return something - 和一个 setter 方法 - model_instance.foo = 'bar'.

Don't confuse attr_accessor with attr_accessible. Accessor is built into Ruby and defines a getter method - model_instance.foo # returns something - and a setter method - model_instance.foo = 'bar'.

Accessible 是由 Rails 定义的,并且使属性可以批量赋值(与 attr_protected 相反).

Accessible is defined by Rails and makes the attribute mass-assignable (does the opposite of attr_protected).

如果 first_name 是模型数据库表中的一个字段,则 Rails 已经为该属性定义了 getter 和 setter.您需要做的就是添加 attr_accessible :first_name.

If first_name is a field in your model's database table, then Rails has already defined getters and setters for that attribute. All you need to do is add attr_accessible :first_name.

这篇关于“警告:不能批量分配受保护的属性"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆