【发布时间】:2011-04-26 01:11:17
【问题描述】:
我使用 RESTful 技术来生成模型(事实上,我正在使用 Devise gem,它为我做这件事),并且我在模型中添加了名为 first_name 和 last_name 的新字段。迁移顺利。我将 attr_accessor :first_name, :last_name 添加到模型中,并希望它能够正常工作。但是当我尝试使用 Doctor.create({:first_name=>"MyName"}) 等批量分配新实例时,我收到错误消息说我无法批量分配受保护的属性。
我认为使用 attr_accessor 的全部意义在于绕过模型字段的保护。你能帮我理解这条信息吗?
编辑:哦,顺便说一下,记录也没有被创建。我认为它们应该是,因为这只是一个警告,但它们不在数据库中。
Edit2:这是我的模型
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 表中创建的,而users 表是医生的祖先。我使用了单表继承。
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 方法,但它有效。
%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")
【问题讨论】:
-
我对 Rails 4 还不是很了解,但我认为这个问题是 Rails 3 的问题。 Rails 4 中
config/application.rb中的默认硬编码配置为空白!
标签: ruby-on-rails activerecord devise