我正在構建一個Rails應用程序,它具有用戶模型,通過使用UserInterests連接模型的has_many:through關係連接到興趣模型。困難的軌道ActiveModel查詢
給定一組興趣,我想要做的是找到所有已選擇至少一組興趣的用戶,或者根本沒有選擇任何興趣的用戶。我在想這樣的事情:
users = users.joins(:user_interests).where(["COUNT(user_interests) = 0 OR user_interests.interest_id IN ?", interest_ids])
這會引發Mysql語法錯誤。任何想法如何實現這一目標?
非常感謝
顯示Mysql的 – rick
的Mysql ::錯誤的完整的錯誤:無效使用組功能:SELECT' users'。* FROM'users' INNER JOIN'user_interests' ON'user_interests'.'user_id' ='users'.'id' WHERE(COUNT(user_interests)= 0或user_interests.interest_id IN(1)) – AdamP