2014-10-19 2 views
0

같은 레코드가 여러 번 정렬되고 매번 순서가 이전 주문과 독립적으로 수행되는 일종의 연역적 쿼리를 만들어야합니다.동일한 레코드 세트를 두 번 주문하는 방법은 두 번째 순서가 첫 번째 레코드와 독립적으로 수행됩니까?

예를 들면 :

tallest_trees = Trees.order(:height => :desc).limit(10) # Gets 10 of the tallest trees out of all available trees 
tallest_trees.order(:width => :desc) # From the 10 of the tallest trees, gets the widest trees **without** any regard to their height 

나는 위와 유사한 방법으로 그것을 시도했지만, 결과는 항상이 경우, 주문을 시도하는 것이왔다, 나무, 모두 동시에 높이와 너비로, 이는 내 필요와 반대입니다.

ActiveRecord 또는 SQL을 직접 사용하는 것과 관련하여 차이점은 없습니다. 대답은 내가 어떻게 든 이전 쿼리를 "안정화"해야만 두 번째 쿼리가 계속 진행되지 않고 새로운 쿼리를 다시 주문해야한다는 것입니다.

답변

0

두 가지 방법이 있습니다. 첫째 :

base_scope = Trees.limit(10) 
tallest_trees = base_scope.order(height: :desc) 
widest_trees = base_scope.order(width: :desc) 

둘째 - reorder 방법 :

tallest_trees = Trees.order(height: :desc).limit(10) 
widest_trees = tallest_trees.reorder(width: :desc) 
+0

'이 그것입니다 reorder'! 감사! – Kasperi

관련 문제