2013-07-27 3 views
1

의 순서를 할까?어떻게이 쿼리를 배열

completed_timeline = 
case_timeline 
.select{|task| task.task_status_name == "Completed" } 
.order('end_date') 

하지만 오류를주고 : 나는이 시도

undefined method `order' for #<Array:0x000000051da2e0> 

답변

5

당신은 선택 호출하기 전에 호출 된 개체의 주문 방법을 원한다.

completed_timeline = 
case_timeline.order('end_date') 
.select{|task| task.task_status_name == "Completed" } 

이 case_timeline 주문에 응답하는 방법을 알고하지 않는 Array 객체를 선택 반환을 호출 할 경우 주문에 응답하는 방법을 이미 알고 액티브 객체를 반환한다는 사실 때문입니다.

+1

도움을 주셔서 대단히 감사드립니다. 효과가있다. – Neha

0

select 블록을 사용하면 ActiveRecord::RelationArray으로 변환합니다. the docs에서 : task_status_name 이후

You can pass a block so it can be used just like Array#select . This builds an array of objects from the database for the scope, converting them into an array and iterating through them using Array#select .

가 열을 것으로 보인다, 그것은 완전히 배열로 변환 피하기 위해 where을 수행하는 가장 좋은 것입니다 : 또는

completed_timeline = case_timeline.where(task_status_name: 'Completed') 
            .order('end_date') 

,이기 위하여 select 전화를 이동 이후 모두 query method로 전화하십시오.

관련 문제