2017-03-06 4 views
0

여러 도전 과제가 같은 경우 dategoal 도전 과제는 항상 전에 habit 도전 과제를 표시 할 수 있습니까?날짜순으로 정렬 String MVC?

challenges_controller

def index 
    @challenges = current_user.challenges.order("date ASC") 
    @challenges_by_years = (@challenges).group_by { |t| [t.date.year, t.date.month] } 
end 

challenge.rb

CATEGORY = ['goal', 'habit'] 
scope :goal, -> { where(category: 'goal') } 
scope :habit, -> { where(category: 'habit') } 

도전 기록

#<Challenge:0x007fd464c67b90 
id: 5, 
name: "Write 3 Gratitudes", 
date: Mon, 06 Mar 2017, 
category: "habit", # The string is always either "goal" or "habit". 
user_id: 1, 

답변

1

당신은으로 주문할 수 있습니다 date 먼저 다음 순서로 category 날짜별로 기록과 같은 날짜와 레코드를 주문합니다 위의 쿼리는 다시 범주별로 다시 정렬합니다

@challenges = current_user.challenges.order("date, category") 

'goal''habit'

+0

'카테고리'에 의한 주문은 '습관'이전에 '목표'로 기본 설정되어 있습니다. 글자가 적은 BCC인가 스코어 라인의 순서인가? –

+1

문자열이 알파벳 순서로 정렬되어 있으므로 'g'가 'h'앞에옵니다. –

1
habit보다 작기 때문에 10

입니다.

current_user.challenges.order(:date, :category) # a bit shorter notation, does the same thing