User.rb
has_many :votes
Event.rb
has_many :votes
Vote.rb
belongs_to :user
belongs_to :event
I am trying to render all the Events that the current_user has not voted on.
Consider this situation:
- There are 100 events
Event.includes(:votes).count ==> 100 - The User has voted on 5 events
current_user.votes.count ==> 5 - There are 75 events with at least one vote from other users
- 20 of the events have not received votes from any users
The result I'm looking for should render the 95 events that have not been voted on by the current_user, including events that have not been voted on by any user.
This query gets all the events that NO users have voted on:
Event.includes(:votes).where(:votes => {:id => nil}).references(:votes).count ==> 20
What query can I use to get all the events that have been voted on by users excluding those that have been voted on by current user (should return 75 in the example above)? I tried the below query but it returned 80 (it included events voted on by the current_user):
Event.includes(:votes).where.not(:votes => {:id => current_user}).references(:votes).count ==> 80
- What query can I use to get events with votes, excluding ones that current user voted for
- Can I combine the two queries into one?