【发布时间】:2014-01-24 00:51:53
【问题描述】:
我无法找到直接与 Active Record 的关系过滤 has_many 的最佳方法。我在这里找到了一堆几乎解决问题的帖子,但没有一个答案对我有用。
组:
has_many :taggings, as: :taggable
has_many :tags, :through => :taggings
用户:
has_many :taggings, as: :taggable
has_many :tags, :through => :taggings
我想查找所有标签与单个组标签匹配的用户
以下方法有效,但我宁愿用查询组合列表。
def interest_matches
matched_users = Array.new
self.tags.uniq.each do |tag|
tag.users.map{ |u| matched_users.push(u) unless matched_users.include?(u) }
end
matched_users
end
非常感谢任何建议!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord has-many-through arel