Is it possible to make a has_one relationship work like this?
I would like to be able able to load records like 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 the
has_onerelation of Person by:Do not forget to create a column
favorite_house_idin the table of Person.