ruby on rails - How to make a has_one associated model show up in class attributes() result? -
where person has_one brain, should expect brain show in hash returned by attributes() person? if so, how create happen?
rails console output:
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} the 2 models 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 shows in attributes brain because brains table has person_id column. no such column need exist people table.
not sure thought list associations among attributes, can association names this:
association_names = self.class.reflect_on_all_associations.map(&:name) see doc on reflections
ruby-on-rails associations nested-attributes rails-activerecord has-one
No comments:
Post a Comment