Deprecated warning for Rails 4 has_many with order

In Rails 4, :order has been deprecated and needs to be replaced with lambda scope block as shown in the warning you've posted in the question. Another point to note is that this scope block needs to be passed before any other association options such as dependent: :destroy etc.

Give this a try:

has_many :contents, -> { order(:position) } # Order by :asc by default

To specify order direction, i.e. either asc or desc as @joshua-coady and @wsprujit have suggested, use:

has_many :contents, -> { order 'position desc' }

or, using the hash style:

has_many :contents, -> { order(position: :desc) }

Further reference on Active Record Scopes for has_many.


It took me a while to figure out how to do order and include, I eventually found that you chain the scope statements,

has_many :things, -> { includes(:stuff).order("somedate desc") }, class_name: "SomeThing"

Just thought I'd add that if you have any option hash arguments, they have to go after the lambda, like this:

has_many :things, -> { order :stuff }, dependent: :destroy

Took me a minute to figure this out myself - hopefully it helps anyone else coming to this question having the same problem.