Jorge Roberto Jorge Roberto - 4 months ago 21
Ruby Question

Uniq method active record ordering

When use .uniq (Active Record Query Method), he any way are ordening my result in array. i need remove ordering when use uniq.

I need to keep order using .uniq method, how can do 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>,
]

Answer

Use order along with uniq:

# assuming you want to sort by name

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

Use :asc instead of :desc to reverse order. See Active Record Query Interface docs for more.