我正在尝试获取具有唯一值的记录.Below是我在使用后获得的输出
VoucherNumber.where(:goodie=> 2917)
[#<VoucherNumber id: 559, vendor_id: 5, voucher_number: "1", trip_id: nil, expiry_date: "2016-03-23 18:30:00", goodie_id: 2917, status: "UN", amount: #<BigDecimal:8f9b7f0,'0.122E3',9(36)>, created_at: "2016-03-04 11:37:22", updated_at: "2016-03-04 11:37:22">, #<VoucherNumber id: 565, vendor_id: 5, voucher_number: "4", trip_id: nil, expiry_date: "2016-03-23 18:30:00", goodie_id: 2917, status: "UN", amount: #<BigDecimal:8faf750,'0.122E3',9(36)>, created_at: "2016-03-04 11:37:22", updated_at: "2016-03-04 11:37:22">, #<VoucherNumber id: 567, vendor_id: 11, voucher_number: "sss", trip_id: nil, expiry_date: "2016-03-22 18:30:00", goodie_id: 2917, status: "UN", amount: #<BigDecimal:8fad7c0,'0.112E3',9(36)>, created_at: "2016-03-04 11:37:22", updated_at: "2016-03-04 11:37:22">, #<VoucherNumber id: 569, vendor_id: 11, voucher_number: "22", trip_id: nil, expiry_date: "2016-03-22 18:30:00", goodie_id: 2917, status: "UN", amount: #<BigDecimal:902d510,'0.112E3',9(36)>, created_at: "2016-03-04 11:37:22", updated_at: "2016-03-04 11:37:22">]..
But expected output is below output
[#<VoucherNumber id: 559, vendor_id: 5, voucher_number: "1", trip_id: nil, expiry_date: "2016-03-23 18:30:00", goodie_id: 2917, status: "UN", amount: #<BigDecimal:8f9b7f0,'0.122E3',9(36)>, created_at: "2016-03-04 11:37:22", updated_at: "2016-03-04 11:37:22"> #<VoucherNumber id: 567, vendor_id: 11, voucher_number: "sss", trip_id: nil, expiry_date: "2016-03-22 18:30:00", goodie_id: 2917, status: "UN", amount: #<BigDecimal:8fad7c0,'0.112E3',9(36)>, created_at: "2016-03-04 11:37:22", updated_at: "2016-03-04 11:37:22">]
有人可以指出我正确的方向吗?
答案 0 :(得分:0)
您可以调用uniq
方法,确保您不会两次获得相同的记录。
VoucherNumber.where(:goodie=> 2917).uniq
VoucherNumber.where(:goodie => 2917).pluck(:vendor_id).uniq
答案 1 :(得分:0)
您可以尝试这种方式,但只返回vendor_id
VoucherNumber.where(:goodie=> 2917).uniq.pluck(:vendor_id)
您可以使用goodie
和vendor_id
uniq
VoucherNumber.group("goodie,vendor_id")
对于选定的礼物:
VoucherNumber.where(:goodie=> 2917).group(:vendor_id)
答案 2 :(得分:-1)
使用此代码:
VoucherNumber.where(:goodie=> 2917).group('goodie, vendor_id')
答案 3 :(得分:-1)
在该结果上尝试group_by vendor_id
然后uniq
。
VoucherNumber.where(:goodie=> 2917).select("DISTINCT(vendor)")
查询是,
SELECT DISTINCT(vendor) FROM "voucher_numbers" WHERE "voucher_numbers"."goodie" = 2917