我收到类似的错误
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
我的艺术家模型我写范围
scope :approved, where("admin_approved = ?", true)
scope :completed_profile, joins(:photos).select("artists.*,count(photos.id) as ctr").group("artists.id").having("ctr >= 2")
在我的控制器中我写
def artists_completed_profile
@artists = Artist.approved.completed_profile.page(params[:page]).per(10)
@total_artists = @artists.size
end
注意: 当我在控制台中尝试时,我没有收到任何错误,但是当我在模型或控制器中写入时,我会收到此错误。
提前致谢