我有两个模型,Person
和Brain
。 Person
has_one
:brain
和Brain
belongs_to
:person
。我想通过/ person / update分配Brain
属性。
class Person < ActiveRecord::Base
has_one :brain
attr_accessible :name
attr_accessible :brain
accepts_nested_attributes_for :brain
end
class Brain < ActiveRecord::Base
belongs_to :person
attr_accessible :weight_kg
attr_accessible :person
accepts_nested_attributes_for :person
end
在Rails控制台中,我可以分配给Person.brain
:
> p = Person.first
=> #<Person id: 1, name: "Dave", created_at: "2013-02-14 20:17:35", updated_at: "2013-02-14 20:17:35">
> p.brain.weight_kg = 5.0
Brain Load (0.2ms) SELECT "brains".* FROM "brains" WHERE "brains"."person_id" = 1 LIMIT 1
=> 5.0
> p.save
(0.6ms) begin transaction
(0.6ms) UPDATE "brains" SET "weight_kg" = 5.0, "updated_at" = '2013-02-14 20:18:11.010544' WHERE "brains"."id" = 1
(317.6ms) commit transaction
=> true
通过网络表单(并通过控制台)我不能,因为陈旧错误,“无法大量分配受保护的属性:brain_attributes ”。
我在attr_accessible :weight_kg
中有Brain
,在Person
accepts_nested_attributes_for :brain
我有{{1}},所以我(错误地)希望这可行。
我错过了什么?
答案 0 :(得分:2)
将attr_accessible更改为:
attr_accessible :brain_attributes