我已经使用RESTful技术来生成模型(实际上,我使用的是Devise gem,这对我来说是这样),并且我添加了名为first_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和姓氏,因为它们在用户表,这是医生的祖先创造。我使用单表继承。
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 protected]", :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'中的默认硬编码配置是空白的! – 2014-03-21 02:25:32
http://apidock.com/rails/ActiveRecord/Base/attr_accessible/class – shilovk 2014-08-31 13:21:40