我有这个relationsship在many_to_many关联中作用范围?
class House
has_many :apartments
end
class Apartments
belongs_to :house
has_many :category_join_table
has_many :categories, :through => :category_join_table
end
我在我的房子模型的目的作出了范围方法“只显示与租房”
scope :with_apartments, lambda { joins(:appartments).group('appartments.id').uniq { |h| h[:id] }}
所以我可以在控制器逻辑house.with_apartments 。这工作正常,所以我只得到与页面上的公寓房子。没有公寓的房子没有显示。
但现在我想做一个关联到我的分类模型(many_to_many)。所以“只显示与公寓和类别= X家”
澄清你的问题:你正在努力实现以下灵活性:'@ some_category.houses'? – jdoe 2012-04-20 09:09:46
是的。与房屋协会。在我的模板页面上,如果有这个 - @ houses.each do | house | - house.appartments.each do | a | #{a.guests} – Remco 2012-04-20 09:11:46
您可以像这样加入多个关联,连接(:appartments =>:categories),然后附加一些查询。 – Yanhao 2012-04-20 09:27:29