Deprecated warning for Rails 4 has_many with order

前端 未结 5 1847
情话喂你
情话喂你 2020-11-27 14:08
class RelatedList < ActiveRecord::Base
  extend Enumerize

  enumerize :list_type, in: %w(groups projects)

  belongs_to :content
  has_many :contents, :order =&g         


        
5条回答
  •  迷失自我
    2020-11-27 14:50

    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) }
    

    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.

提交回复
热议问题