Rails: How to find objects that exceed a certain count -
i trying query want find users have exceed number of violations. in user model each user has_many violation
. in index action want find users have more 25 violations. have in users controller:
update
if params[:viewpage] == "violators" @count = "25".to_i @users = user.includes(:violation).group('violation.user_id').having("count(*) >= #{@count}").paginate :page => params[:page], :per_page => 20 elsif......
this gives me error: invalid value integer(): "violators"
you can use group
, having
.
user.includes(:violations).group('violations.user_id').having("count(violations.user_id) >= #{@count}")
of course, if not have control on input, want sanitize input before inject string.
Comments
Post a Comment