문제

My Rails 3.2.8 앱에서는 몇 가지 상황에서 함께 체인하고 싶습니다.

그래서, 예를 들어,이 두 범위가 있습니다.

scope :by_status, lambda { |status| if status == "All" then WorkRequest.all else WorkRequest.find_all_by_status(status) end }
scope :in_date_range, lambda { |start_date, end_date| includes([:person, :pier_module]).where("(status_date >= ?) AND (status_date <= ?)", start_date, end_date) }
.

나는 별도로 사용하지만, 나는 또한 이것을 이렇게 전화 할 수 있습니다 :

WorkRequest.by_status("Accepted").in_date_range("2012-01-01", "2012-10-02")
.

in_date_range가 배열 방법이 아니라는 것을 불평하는 것을 알 수 있습니다.

그러나 다른 범위가 있습니다

scope :active, includes([:person, :pier_module]).where("status = 'New Request'")
.

그리고 내가 그렇게하면

WorkRequest.active.in_date_range("2012-01-01", "2012-10-02")
.

그것은 작동합니다! 분명히 활성 범위는 관계를 반환하는 반면 람다 스코프가 배열을 반환 할 수 없으므로 연결할 수 없습니다.

더 간단한 범위와 람다 스코프의 차이점, 매개 변수가 어떻게 영향을 미치는지, 내가 해낸 결합 된 범위를 작성하는 것을 짧게 할 수있는 일이 있는지 여부를 알고 싶습니다.

scope :by_status_in_date_range, lambda { |status, start_date, end_date|  includes([:person, :pier_module]).where("(status = ?) AND (status_date >= ?) AND (status_date <= ?)", status, start_date, end_date) }
.

는 작동하지만 (개별 범위가 필요하기 때문에) 또는 Rails-Ish가 필요하지 않습니다. 여기에서 검색하면 비슷한 질문을 보았지만이 상황에 적용되는 것처럼 보이지 않는 것처럼 보이는 것입니다.

도움이 되었습니까?

해결책

범위에서

scope :by_status, lambda { |status| if status == "All" then WorkRequest.all else WorkRequest.find_all_by_status(status) end }
.

metods allfind_all_by_status Array 대신 ActiveRecord::Relation를 반환합니다.예를 들어 where로 교체해야합니다.

scope :by_status, lambda { |status| where(:status => status) unless status == "All" }
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top