我正在实施群组功能。我被困在寻找一个小组的成员。 让我们说
Class Group
has_many :group_memberships
has_many :members, :source => :user, :through => :group_memberships
end
Class GroupMembership
belongs_to :group
belongs_to :user
end
Class User
has_many :group_memberships
has_many :groups, :through => :group_memberships
has_one :profile, :dependent => :destroy
end
Class Profile
belongs_to :user
end
如何使用个人资料中的可搜索字段搜索群组成员
答案 0 :(得分:1)
下面的内容可以为你工作吗?
group = Group.find(id)
users = group.users.joins(:profiles).where("profile.age>18")
答案 1 :(得分:0)
您可以在急切加载的帮助下完成此操作 - 只需在预先加载的关联上指定条件,例如 -
group = Group.last
users = group.users.includes(:profiles).where("profiles.name" => "xyz")
有关详细信息,请参阅 - http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-eager-loaded-associations