Anyone know how I can create this query using ActiveRecord rather than using SQL?
@leaderboard = ActiveRecord::Base.connection.execute("SELECT name, street, town, county, avg(volume_used) FROM `DBName`.`events
join DBName.households on idhouseholds = events.household_id
group by household_id
order by volume_used ASC;");
How about…
assuming that by
idhouseholdsyou actually meanthouseholds.idand that you want to order by the aggregate average field you are selecting.