I am trying to do a database query where it only returns results where a has_many relationship isn’t empty. Below is an example where the has_many :gift_images :
Gift.joins(:gift_images).includes(:gift_images, :comments, :category, :user).select("gifts.*, COUNT(gift_images.id) AS gcount").where("gcount > 0")
Something like that, but it doesn’t work. Any ideas?
You can’t use aliases on where statements, try the following