ruby on rails - Uniq method active record ordering -
when use .uniq (active record query method), way ordening result in array. need remove ordering when use uniq.
i need keep order using .uniq method, how can solve this?
without .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> ]
with .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>, ]
use order
along uniq
:
# assuming want sort name coupon.where(...).uniq.order(name: :desc)
use :asc
instead of :desc
reverse order. see active record query interface docs more.
Comments
Post a Comment