Converting an SQL query to a Rails AREL query?
问题 I have a working SQL query thanks to the help of Erwin Brandstetter in my earlier question 'Order with a has_many relationship'. How would I turn this SQL into an ActiveRecords or AREL query for use in a scope? SELECT a.* FROM articles a LEFT JOIN ( SELECT DISTINCT ON (article_id) article_id, value FROM metrics m WHERE name = 'score' ORDER BY article_id, date_created DESC ) m ON m.article_id = a.id ORDER BY m.value DESC; The closest I have come is with a friends help... scope :highest_score,