I’m trying to count the number of rows with a distinct column with Active Record.
The following SQL works and gives me the correct result:
SELECT COUNT(DISTINCT(user_id)) FROM attempts WHERE score = 100 AND problem_id = 1
But this code throws an ActiveRecord::StatementInvalid error:
attempts.where(:score => 100).count(:distinct => :user_id)
Try:
more infos: http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html