I found some topics here on SO, but I still can't find the right setup for my query.
This is query, that works me well on localhost:
@cars = Car.find_by_sql('SELECT cars.*, COUNT(cars.id) AS counter
FROM cars
LEFT JOIN users ON cars.id=users.car_id
GROUP BY cars.id ORDER BY counter DESC')
But on Heroku gives me the error above - GROUP BY clause or be used in an aggregate function.
Then I have read somewhere, that I should specify all columns in the table, so I tried this:
@cars = Car.find_by_sql('SELECT cars.id, cars.name, cars.created_at,
cars.updated_at, COUNT(cars.id) AS counter
FROM cars
LEFT JOIN users ON cars.id=users.car_id
GROUP BY (cars.id, cars.name, cars.created_at, cars.updated_at)
ORDER BY counter DESC')
But this doesn't work on localhost and also not on Heroku...
What should be the right config of the query?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…