ruby - rails - Find objects with duplicate attributes -
if have array of object foo
how can find , return objects duplicate values specific attributes? instance, want return objects have duplicate values both foo.x
, foo.y
i using rails 3.2 , ruby 1.9
i looking like:
one = foo.create!(:x => 1, :y => 2, :z => 3) 2 = foo.create!(:x => 1, :y => 2, :z => 6) 3 = foo.create!(:x => 4, :y => 2, :z => 3) arr = [one, two, three] arr.return_duplicates_for_columns(foo.x, foo.y) = [one, two]
i'm not sure solution or how work you, should work.
foos = foo.some_ar_query_that_returns_some_foos grouped_foos = foo.group_by {|f| [f.x, f.y]}
grouped_foos hash. keys array of x , y values. values array of foo instances same values. element of hash size of value more 1 has duplicates.
Comments
Post a Comment