任何人都知道如何使用ActiveRecord而不是使用SQL創建此查詢?ActiveRecord查詢
@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;");
您的ActiveRecord連接將會到特定的數據庫。在運行查詢之前,您需要連接到'DBName'(通過在database.yml中指定正確的參數)。 – Ben