Rails 4 default scope

前端 未结 8 2098
萌比男神i
萌比男神i 2020-12-04 15:21

In my Rails app have a default scope that looks like this:

default_scope order: \'external_updated_at DESC\'

I have now upgraded to Rails 4

相关标签:
8条回答
  • 2020-12-04 15:48

    Should be only:

    class Ticket < ActiveRecord::Base
      default_scope -> { order(:external_updated_at) } 
    end
    

    default_scope accept a block, lambda is necessary for scope(), because there are 2 parameters, name and block:

    class Shirt < ActiveRecord::Base
      scope :red, -> { where(color: 'red') }
    end
    
    0 讨论(0)
  • 2020-12-04 15:50

    This also worked for me:

    default_scope { order('created_at DESC') }

    0 讨论(0)
  • This works for me in Rails 4

    default_scope { order(external_updated_at: :desc) }
    
    0 讨论(0)
  • 2020-12-04 16:01
    default_scope -> { order(created_at: :desc) }
    

    Don't forget the -> symbol

    0 讨论(0)
  • 2020-12-04 16:06

    This worked from me (just for illustration with a where) because I came to this topic via the same problem.

    default_scope { where(form: "WorkExperience") }
    
    0 讨论(0)
  • 2020-12-04 16:06
    default_scope { 
          where(published: true) 
    }
    

    Try This.

    0 讨论(0)
提交回复
热议问题