Uniq方法主动记录排序

时间:2016-07-15 18:45:11

标签: ruby-on-rails ruby

当使用 .uniq (Active Record Query Method)时,他无论如何都会在数组中输入结果。我在使用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>,
 ]

1 个答案:

答案 0 :(得分:1)

使用orderuniq

# assuming you want to sort by name

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

使用:asc代替:desc来改变订单。有关详情,请参阅Active Record Query Interface文档。