4 ms·
I haven't used Rails or ActiveRecord for a long time, but from memory it gives you an escape hatch in the form of Arel that lets you represent parts of your que
by strken 4y ago
I haven't used Rails or ActiveRecord for a long time, but from memory it gives you an escape hatch in the form of Arel that lets you represent parts of your query as SQL while still using the ORM for the rest.
Edit: I think it's something like
Album
.select(:id, :name, "SUM(songs.length) AS total_length")
.left_outer_joins(:songs)
.group(:id)
.order("SUM(songs.length) ASC")
I think you don't even need to explicitly wrap it in Arel.
- kayodelycaon 4y agoI use rails every day. Looks close enough. You’d just need to specify the fields with the table name, I.e. ‘albums.id’. Also, for anyone reading, a total_length method dynamically will be added objects returned. :)
- slimdave 4y ago