是否可以使has_one关系像这样工作?
我希望能够加载这样的记录:
@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
答案 0 :(得分:0)
将Person的has_one
关系替换为:
belongs_to :favorite_house, :class_name => "House"
不要忘记在Person表格中创建一个列favorite_house_id
。