1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Implement hash equality for WhereClause

Co-authored-by: Ryuta Kamizono <kamipo@gmail.com>
This commit is contained in:
John Hawthorn 2021-03-04 15:48:56 -08:00
parent 1baa0684a0
commit 3bac3a4e8b
2 changed files with 18 additions and 0 deletions

View file

@ -76,6 +76,11 @@ module ActiveRecord
other.is_a?(WhereClause) && other.is_a?(WhereClause) &&
predicates == other.predicates predicates == other.predicates
end end
alias :eql? :==
def hash
[self.class, predicates].hash
end
def invert def invert
if predicates.size == 1 if predicates.size == 1

View file

@ -245,6 +245,19 @@ class ActiveRecord::Relation
assert_equal only_common, common_with_extra.or(only_common) assert_equal only_common, common_with_extra.or(only_common)
end end
test "supports hash equality" do
h = Hash.new(0)
h[WhereClause.new(["a"])] += 1
h[WhereClause.new(["a"])] += 1
h[WhereClause.new(["b"])] += 1
expected = {
WhereClause.new(["a"]) => 2,
WhereClause.new(["b"]) => 1
}
assert_equal expected, h
end
private private
def table def table
Arel::Table.new("table") Arel::Table.new("table")