program story

Rails 4 기본 범위

inputbox 2020. 10. 23. 07:52
반응형

Rails 4 기본 범위


내 Rails 앱에는 다음과 같은 기본 범위가 있습니다.

default_scope order: 'external_updated_at DESC'

이제 Rails 4로 업그레이드했으며 "해시를 사용하여 #scope 또는 #default_scope 호출은 더 이상 사용되지 않습니다. 범위가 포함 된 람다를 사용하십시오."라는 사용 중단 경고가 표시됩니다. 다른 범위를 성공적으로 변환했지만 default_scope의 구문이 무엇인지 모르겠습니다. 작동하지 않습니다.

default_scope, -> { order: 'external_updated_at' }

다음과 같아야합니다.

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

default_scope는 블록을 받아들이고, 람다는 scope ()에 두 개의 매개 변수, name과 block이 있기 때문에 필요합니다.

class Shirt < ActiveRecord::Base
  scope :red, -> { where(color: 'red') }
end

이것은 나를 위해 일한 것입니다.

default_scope  { order(:created_at => :desc) }

이것은 또한 나를 위해 일했습니다.

default_scope { order('created_at DESC') }


나는 같은 문제를 통해이 주제에 왔기 때문에 (어디에 대한 설명을 위해) 저에게 효과적이었습니다.

default_scope { where(form: "WorkExperience") }

lambda키워드를 사용할 수도 있습니다 . 여러 줄 블록에 유용합니다.

default_scope lambda {
  order(external_updated_at: :desc)
}

이는

default_scope -> { order(external_updated_at: :desc) }

default_scope { order(external_updated_at: :desc) }

이것은 Rails 4에서 나를 위해 작동합니다.

default_scope { order(external_updated_at: :desc) }

default_scope -> { order(created_at: :desc) }

->상징을 잊지 마세요


default_scope { 
      where(published: true) 
}

이 시도.

참고 URL : https://stackoverflow.com/questions/18506038/rails-4-default-scope

반응형