order での 文字列指定とシンボル指定の差についての情報を示します。
研究してみてください。
symbol で渡した場合と文字列で渡したときは、跛行される SQL にすこし差があります。
- ActiveRecord::QueryMethods order
https://apidock.com/rails/ActiveRecord/QueryMethods/order
...
User.order(:name)
=> SELECT "users".* FROM "users" ORDER BY "users"."name" ASC
User.order('name')
=> SELECT "users".* FROM "users" ORDER BY name
...
シンボル指定した場合、 SQL には name でなく users.name となります。
join や merge でこの差が効いてきます。
- Rails Quick Tips - ActiveRecord Ordering
https://hashrocket.com/blog/posts/rails-quick-tips-activerecord-ordering
...
See the difference? When the symbol is passed, the query is built using fully qualified and properly quoted column names.
Most of the time your query will run fine but this does matter!
The issue comes up when you start joining tables or merging queries.
Without the column being fully qualified with the table name, your query may produce an ambiguous match.
This occurs when your joined tables have columns with the same name.
...
join したときの order 指定をシンボルで行う例が以下にあります。
- Rails order by association field
https://stackoverflow.com/questions/19616611/
...
User.includes(:user_extension).order("user_extensions.company desc")
...
User.joins(:user_extension).merge(UserExtension.order(company: :desc))
...
文字列では order("user_extensions.company desc") とする必要がありますが、
シンボルでは order(company: :desc) とできます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/11 01:00