activerecord - rails 3.1 - has_one, through -



activerecord - rails 3.1 - has_one, through -

is possible create has_one relationship work this?

i able able load records this:

@person = person.find(1) => {person id: 1, favorite_house_id: 10} @person.favorite_house => {house id: 10....) class person < activerecord::base has_many :houses, through: :person_houses has_one :favorite_house, through: :person_houses end class personhouse < activerecord::base belongs_to :house belongs_to :person end class house < activerecord::base has_many :people, through: :person_houses end

replace has_one relation of person by:

belongs_to :favorite_house, :class_name => "house"

do not forget create column favorite_house_id in table of person.

ruby-on-rails-3 activerecord

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -