diff --git a/CHANGELOG.md b/CHANGELOG.md index 86aa17a1..8b759d84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # active_hash Changelog +## next - unreleased + +- Fix relation matching when attribute name collides with a method. [#281](https://github.com/active-hash/active_hash/pull/281) @flavorjones + + ## Version [3.2.0] - 2022-07-14 - Add Ruby 3.2 to the CI matrix [#275](https://github.com/active-hash/active_hash/pull/275) @petergoldstein diff --git a/lib/active_hash/condition.rb b/lib/active_hash/condition.rb index 27fa3276..cf78b052 100644 --- a/lib/active_hash/condition.rb +++ b/lib/active_hash/condition.rb @@ -19,7 +19,7 @@ def matches?(record) expectation_method = inverted ? :any? : :all? constraints.send(expectation_method) do |attribute, expected| - value = record.public_send(attribute) + value = record.read_attribute(attribute) matches_value?(value, expected) end @@ -41,4 +41,4 @@ def matches_value?(value, comparison) def normalize(value) value.respond_to?(:to_s) ? value.to_s : value end -end \ No newline at end of file +end diff --git a/spec/active_hash/relation_spec.rb b/spec/active_hash/relation_spec.rb index f788bc42..432286d1 100644 --- a/spec/active_hash/relation_spec.rb +++ b/spec/active_hash/relation_spec.rb @@ -50,4 +50,25 @@ expect(array.size).to eq(2) end end + + describe "colliding methods https://github.com/active-hash/active_hash/issues/280" do + it "should handle attributes named after existing methods" do + klass = Class.new(ActiveHash::Base) do + self.data = [ + { + id: 1, + name: "Aaa", + display: true, + }, + { + id: 2, + name: "Bbb", + display: false, + }, + ] + end + + expect(klass.where(display: true).length).to eq(1) + end + end end