在哪里Person
has_one
Brain
,我应该期望brain
出现在for返回的哈希 attributes()
中Person
?如果是这样,如何做到这一点?
Rails 控制台输出:
1.9.3p327 :003 > Person.new.attributes
=> {"id"=>nil, "name"=>nil, "created_at"=>nil, "updated_at"=>nil}
1.9.3p327 :004 > Brain.new.attributes
=> {"id"=>nil, "weight_kg"=>nil, "created_at"=>nil, "updated_at"=>nil, "person_id"=>nil}
这两个模型是Person
和Brain
:
class Person < ActiveRecord::Base
has_one :brain
attr_accessible :name
attr_accessible :brain
attr_accessible :brain_attributes
accepts_nested_attributes_for :brain
end
class Brain < ActiveRecord::Base
belongs_to :person
attr_accessible :weight_kg
attr_accessible :person
attr_accessible :person_attributes
accepts_nested_attributes_for :person
end
person_id
显示在属性中,Brain
因为该brains
表有一个person_id
列。该表不需要这样的列people
。