문제

나는 당신이 레일로 지명 된 스코프를 만들 수 있다는 것을 알고 있습니다. 이로 인해 나중에 구축 할 수있는 조건을 지정할 수 있습니다.

named_scope :active, :conditions => {:active => true}

...

MyModel.active.find(...)

이것은 나중에까지 평가되지 않는 프록시 객체를 만들어 작동합니다. 내가 알고 싶은 것은 역학을 만들 수 있는지 여부입니다. un이름이 지정된 SCOPE?

내 말은, 내가 갈 수있는 방법 'foo'가 있습니까?

scope = MyModel.foo(:conditions => {:target_id => 4})

그리고 통과합니다 scope 내가 더 할 수있는 프록시 개체로 .findS 또는 다른 범위의 전화?

도움이 되었습니까?

해결책

예, 확인하십시오 익명의 범위:

def find_products
  scope = Product.scoped({})
  scope = scope.conditions "products.name LIKE ?", "%#{keywords}%" unless keywords.blank?
  scope = scope.conditions "products.price >= ?", minimum_price unless minimum_price.blank?
  scope = scope.conditions "products.price <= ?", maximum_price unless maximum_price.blank?
  scope = scope.conditions "products.category_id = ?", category_id unless category_id.blank?
  scope
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top