I am building a Rails app which has a User model, joined to a Interests model through a has_many :through relationship using a UserInterests join model.
Given a set of interests, what I want to do is find all users who have selected at least one interest in that set, OR who have not selected any interests at all. I am thinking of something a bit like this:
users = users.joins(:user_interests).where(["COUNT(user_interests) = 0 OR user_interests.interest_id IN ?", interest_ids])
This raises a Mysql syntax error. Any ideas how to achieve this?
Many thanks