我正在构建一个Rails应用程序,它有一个用户模型,通过使用UserInterests连接模型的has_many :through关系连接到兴趣模型。
给定一组兴趣,我想要做的是找到在该组中至少选择了一个兴趣的所有用户,或者根本没有选择任何兴趣的用户。我正在考虑类似这样的事情:
users = users.joins(:user_interests).where(["COUNT(user_interests) = 0 OR user_interests.interest_id IN ?", interest_ids])
这会引发 Mysql 语法错误。有什么想法可以实现这一点吗?
非常感谢
最佳答案
试试这个,
users = User.joins(:user_interests).where("COUNT(user_interests) = 0 OR interest_id IN (?)", interest_ids)
关于mysql - 困难的 Rails ActiveModel 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29983008/