레일에서 *un *명명 스코프를 만들 수 있습니까?
-
19-09-2019 - |
문제
나는 당신이 레일로 지명 된 스코프를 만들 수 있다는 것을 알고 있습니다. 이로 인해 나중에 구축 할 수있는 조건을 지정할 수 있습니다.
named_scope :active, :conditions => {:active => true}
...
MyModel.active.find(...)
이것은 나중에까지 평가되지 않는 프록시 객체를 만들어 작동합니다. 내가 알고 싶은 것은 역학을 만들 수 있는지 여부입니다. un이름이 지정된 SCOPE?
내 말은, 내가 갈 수있는 방법 'foo'가 있습니까?
scope = MyModel.foo(:conditions => {:target_id => 4})
그리고 통과합니다 scope
내가 더 할 수있는 프록시 개체로 .find
S 또는 다른 범위의 전화?
해결책
예, 확인하십시오 익명의 범위:
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
제휴하지 않습니다 StackOverflow