我已经使用基于REST的技术来生成模型(其实,我使用设计的宝石,它会替我),我也增加了新的领域被称为FIRST_NAME和LAST_NAME到模型。迁移了罚款。我加了attr_accessor:FIRST_NAME,:姓氏的模型,并预计这将只是工作。但是,当我尝试用Doctor.create大规模分配新实例({:如first_name =>MYNAME})等,我得到的错误说我不能大规模分配的保护属性。
我想用attr_accessor整点是要解决一个模型领域的protectedness。你能帮我做这个消息的意义?
编辑:哦,顺便记录不获取创建无论是。我认为他们应该是因为这只是一个警告,但他们不是在数据库上。
EDIT2:这里是我的模型
类博士<用户
的has_many:患者
的has_many:prescriptions,:通过=> :患者
validates_ presence_of:邀请函,:上=> :创建:消息=> 不能为空
attr_accessor:邀请函
结束
和架构,其不具有如first_name和last_name因为它们在用户表,这是医生的祖先产生。我使用的单表继承。
CREATE_TABLE:医生做| T |
t.integer:邀请函
t.timestamps
结束
和这是迁移改变用户表
add_column:用户:FIRST_NAME,:字符串
add_column:用户:姓氏,:字符串
add_column:用户:类型:字符串
编辑:这里是种子文件。我不包括truncate_db_table方法,但它的工作原理。
%W {医生病人}。每做| M |
truncate_db_table(米)
结束
Doctor.create(:邀请=> 5,:电子邮件=>中email@gmail.com:FIRST_NAME =>中名:姓氏=>中姓)
Patient.create(:doctor_id => 1:性别=>中男,:DATE_OF_BIRTH =>中1991年2月24日)
解决方案
不要混淆 attr_accessor
与 attr_accessible
。存取内置Ruby和定义了一个getter方法 - model_instance.foo#返回的东西
- 和setter方法 - model_instance.foo ='吧'
。
无障碍客房是由Rails的定义,使属性大众分配(做相反的 attr_protected
)。
如果 FIRST_NAME
是在模型的数据库表中的字段,然后Rails的已定义的getter和setter的属性。所有你需要做的就是添加 attr_accessible:FIRST_NAME
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.
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?
Edit: 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
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
and this is the migration to change the users table
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :type, :string
EDIT: 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")
解决方案
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 is defined by Rails and makes the attribute mass-assignable (does the opposite of attr_protected
).
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
.
相关推荐
最新文章