I am getting error like
Mysql2::Error: Unknown column 'ctr' in 'having clause': SELECT COUNT(*) AS count_all, artists.id AS artists_id FROM `artists` INNER JOIN `photos` ON `photos`.`photoable_id` = `artists`.`id` AND `photos`.`photoable_type` = 'Artist' WHERE (admin_approved = 1) GROUP BY artists.id HAVING ctr >= 2
My artist model I write scope
scope :approved, where("admin_approved = ?", true)
scope :completed_profile, joins(:photos).select("artists.*,count(photos.id) as ctr").group("artists.id").having("ctr >= 2")
In my controller I write
def artists_completed_profile
@artists = Artist.approved.completed_profile.page(params[:page]).per(10)
@total_artists = @artists.size
end
Note: When I try in my console then I am not getting any error but when I write in model or controller then I am getting this error.
Thanks In Advance
Something in your pagination is doing a
.count, most likely when it tries to figure out the total number of matches and thus the total number of pages. That.countwill ignore the.selectpart of your scope, that’s why you’re seeing:being sent to MySQL. Your
ctralias is defined by your.selectso the.count‘s SQL fails. You should be able to get around this problem by not usingctrin your HAVING, just use the rawcount(photos.id):