저는 ActiveRecord 객체의 두 요소 배열 인 slots_to_import
을 가지고 있습니다. 이러한 개체에는 begin_at
개의 열이 있으며 따라서 특성이 있습니다. 나는 고유 한 begin_at
값을 가진 객체를 얻으려고했습니다. 아아, slots_to_import.uniq_by(&:begin_at)
가 작동하지 않았습니다. 그러나 begin_at
값은 두 개체에 대한 동일 :루비 이상 : x == y && [x, y] .uniq == [x, y]
(rdb:1) p slots_to_import.first.begin_at == slots_to_import.last.begin_at
true
(rdb:1) p slots_to_import.uniq_by(&:begin_at).map(&:begin_at)
[Mon, 26 Nov 2012 19:00:00 UTC +00:00, Mon, 26 Nov 2012 19:00:00 UTC +00:00]
(rdb:1) p [slots_to_import.first.begin_at, slots_to_import.last.begin_at].uniq
[Mon, 26 Nov 2012 19:00:00 UTC +00:00, Mon, 26 Nov 2012 19:00:00 UTC +00:00]
좀 더 주위를 확인 :
(rdb:1) p [slots_to_import.first.begin_at.to_datetime, slots_to_import.last.begin_at.to_datetime].uniq
[Mon, 26 Nov 2012 19:00:00 +0000]
(rdb:1) p [slots_to_import.first.begin_at.usec, slots_to_import.last.begin_at.usec].uniq
[0]
(rdb:1) p [slots_to_import.first.begin_at.to_f, slots_to_import.last.begin_at.to_f].uniq
[1353956400.0]
(rdb:1) p [slots_to_import.first.begin_at.utc, slots_to_import.last.begin_at.utc].uniq
[Mon, 26 Nov 2012 19:00:00 +0000]
(rdb:1) p [slots_to_import.first.begin_at, slots_to_import.last.begin_at].uniq
[Mon, 26 Nov 2012 19:00:00 UTC +00:00, Mon, 26 Nov 2012 19:00:00 UTC +00:00]
나는 (그들이 아니었다 때문에) 아마도 그 UNIQ 그들이 같은 객체가 있었다 여부를 확인 생각했다.
1.8.7 :111 > x = Time.zone.parse("Mon, 29 Oct 2012 19:29:17 UTC +00:00")
=> Mon, 29 Oct 2012 19:29:17 UTC +00:00
1.8.7 :112 > y = Time.zone.parse("Mon, 29 Oct 2012 19:29:17 UTC +00:00")
=> Mon, 29 Oct 2012 19:29:17 UTC +00:00
1.8.7 :113 > x == y
=> true
1.8.7 :114 > [x, y].uniq
=> [Mon, 29 Oct 2012 19:29:17 UTC +00:00]
내가 루비 1.8.7p358 및 ActiveSupport 3.2.0을 사용하고 있습니다 :하지만, 내 레일 콘솔의 일부 noodling는 오브젝트 ID 검사를 사용하지 않는 것을 나에게 보여 주었다. BTW, 난 그냥 to_datetime
추가하여 내 자신의 문제를 해결할 수 있지만 정말 변환이 작동하지 않는 이유는 궁금 해서요.
'x.hash'와'y.hash'의 결과를 비교해 보았습니까? – hammar
@hammar : 'uniq_by'가 Hash 기반이라는 것을 보는 흥미로운 아이디어입니다. –
@hammar'x.hash'와'y.hash'도 같습니다! -'(rdb : 1) p slots_to_import.first.begin_at.hash == slots_to_import.last.begin_at.hash => true' – ehsanul