2016-07-15 75 views
-2

當使用.uniq(活動記錄查詢方法)時,他以任何方式將我的結果排列在數組中。我需要刪除使用uniq時的排序。Uniq方法活動記錄排序

我需要使用.uniq保持訂單的方法,該怎麼辦才能解決這個問題?

沒有.uniq

[#<Coupon:0x0000001cadced0 
    id: 838882461, 
    name: "HOW_TO_CODE_50", 
    token_type: "manual", 
    value: 50, 
    quantity: 5, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001cadc408 
    id: 922059944, 
    name: "HOW_TO_CODE_70", 
    token_type: "manual", 
    value: 70, 
    quantity: 5, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001cae3bb8 
    id: 469697148, 
    name: "LEARN_RUBY_20", 
    token_type: "manual", 
    value: 20, 
    quantity: 10, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001cae3190 
    id: 68100775, 
    name: "HOW_TO_CODE_NOT_USED", 
    token_type: "manual", 
    value: 1000, 
    quantity: 5, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: false, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001cae2998 
    id: 214172726, 
    name: "LEARN_RUBY_30", 
    token_type: "manual", 
    value: 30, 
    quantity: 10, 
    available_until: Thu, 14 Jul 2016 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00> 
    ] 

.uniq

[#<Coupon:0x0000001c786498 
    id: 68100775, 
    name: "HOW_TO_CODE_NOT_USED", 
    token_type: "manual", 
    value: 1000, 
    quantity: 5, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: false, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001c7859d0 
    id: 214172726, 
    name: "LEARN_RUBY_30", 
    token_type: "manual", 
    value: 30, 
    quantity: 10, 
    available_until: Thu, 14 Jul 2016 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001c784fa8 
    id: 387313615, 
    name: "GLOBAL_DEMO", 
    token_type: "manual", 
    value: 75, 
    quantity: 5, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001c784800 
    id: 469697148, 
    name: "LEARN_RUBY_20", 
    token_type: "manual", 
    value: 20, 
    quantity: 10, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
#<Coupon:0x0000001c784008 
    id: 548707893, 
    name: "HOW_TO_CODE_FREE", 
    token_type: "manual", 
    value: 100, 
    quantity: 5, 
    available_until: Sat, 15 Jul 2017 18:01:24 UTC +00:00, 
    percentual: true, 
    school_id: 1, 
    created_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00, 
    updated_at: Fri, 15 Jul 2016 18:01:25 UTC +00:00>, 
] 
+0

我對你的問題有點不清楚。你的意思是你不希望陣列中的元素重新排列uniq?你能讓它變得更清楚嗎? –

+0

是的,我需要保持uniq的順序,而不使用uniq。 –

+0

最新你的問題..? – Vishal

回答

1

使用orderuniq一起:

# assuming you want to sort by name 

Coupon.where(...).uniq.order(name: :desc) 

使用:asc而不是:desc來顛倒訂單。有關更多信息,請參見Active Record Query Interface文檔。