积极记录,找到不同用户的共同兴趣

时间:2016-05-04 14:22:56

标签: ruby-on-rails-4 activerecord

我有User has_many

的课程Interests

现在,当我收到用户时,我希望与其他用户达成共同的兴趣。

我如何处理ActiveRecord?

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

the_user = User.find(1)
interest_ids = the_user.interest_ids
users_with_common_interests = User.includes(:interests)
    .where(interests: { id: interest_ids })
    .where.not(id: the_user)
相关问题