I've seen several answers, but couldn't find anything that helps me
I have a Model which has a country column and a city column.
doing some SQL query, I want to add to that query a group by first country and for each country to group by city
Model.where("name LIKE ?", "%#{query}%").group(["country", "city"])
but I get
PG::GroupingError: ERROR: column "models.id" must appear in the GROUP BY clause or be used in an aggregate function
and I am not sure what to do, since I don't want to group by the ID, just by country and city
I am sure I am missing something here, not sure what
using the fields, as suggested with
Model.where("name LIKE ?", "%#{query}%").group("country, city").select("country, city")
still gives the same error
Model.where("name LIKE ?", "%#{query}%").group("country, city").select("name, city"). While you will be usinggroup by, in any DB, you must need to use those columns which you have used in yourgroup byclause, into theselectclause too.Model.where("name LIKE ?", "%#{query}%").group(["country", "city"]).select("id")selectmethod.select("country, city")instead ofselect("name, city").