diff --git a/lib/active_hash/condition.rb b/lib/active_hash/condition.rb index cf78b05..26dce78 100644 --- a/lib/active_hash/condition.rb +++ b/lib/active_hash/condition.rb @@ -39,6 +39,6 @@ def matches_value?(value, comparison) end def normalize(value) - value.respond_to?(:to_s) ? value.to_s : value + value.respond_to?(:to_s) ? value&.to_s : value end end diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index f105dca..8a9a426 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -465,7 +465,8 @@ class Region < ActiveHash::Base Country.data = [ {:id => 1, :name => "US", :language => 'English'}, {:id => 2, :name => "Canada", :language => 'English'}, - {:id => 3, :name => "Mexico", :language => 'Spanish'} + {:id => 3, :name => "Mexico", :language => 'Spanish'}, + {:id => 5, :name => "Any", :language => nil} ] end @@ -524,6 +525,14 @@ class Region < ActiveHash::Base it "returns nil when passed a wrong id" do expect(Country.find_by(:id => 4)).to be_nil end + + it "finds record by nil value" do + expect(Country.find_by(:language => nil).id).to eq(5) + end + + it "doesn't finds nil records when searching for ''" do + expect(Country.find_by(:language => '')).to be_nil + end end describe ".find_by!" do