Reputation: 3035
I have two models, a Person
and a Brain
. Person
has_one
:brain
, and Brain
belongs_to
:person
. I want to assign Brain
attributes through a /person/ update.
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
In the Rails console I can assign to 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
Via the web form (and via the console) I cannot, because of the well-worn error, "Can't mass-assign protected attributes: brain_attributes".
I have attr_accessible :weight_kg
in Brain
, and in Person
I have accepts_nested_attributes_for :brain
, so I (wrongly) expect this to work.
What am I missing?
Upvotes: 0
Views: 64