ActiveRecord Query
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 v开发者_如何学Pythonolume_used ASC;");
How about...
@leaderboard = Event.select([:name,:street,:town,:county,'avg(volume_used) as avg_used']).joins(:households).group('events.household_id').order('volume_used asc')
assuming that by idhouseholds
you actually meant households.id
and that you want to order by the aggregate average field you are selecting.
精彩评论