Dynamic query order scopes in Rails
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
One of my favourite query scopes in Laravel is latest()
which will order the query by the latest models based on the created_at
column. In addition you can pass the column you want to order on in case you need something else.
I wanted to implement this in Rails but couldn't work out how to use a symbol as a column name for the column_name: :sort_direction
syntax, but soon realised I could use the hashrocket to get it to work the way I wanted.
With this code in your base record model you can easily query your database by a given column. Be careful to consider indexing columns when this is necessary.