diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index eda0a20b..0c062219 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -411,7 +411,7 @@ def [](key) end def _read_attribute(key) - attributes[key] + attributes[key.to_sym] end alias_method :read_attribute, :_read_attribute diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index 314cfdba..f105dca4 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -284,6 +284,13 @@ class Region < ActiveHash::Base expect(record.first.name).to eq('US') end + it "filters records when passed a hash with string keys" do + record = Country.where('name' => 'US') + expect(record.count).to eq(1) + expect(record.first.id).to eq(1) + expect(record.first.name).to eq('US') + end + it "raises an error if ids aren't unique" do expect do Country.data = [ @@ -1194,12 +1201,24 @@ class Region < ActiveHash::Base expect(country._read_attribute(:foo)).to eq(:bar) end + it "works when string key passed to _read_attribute" do + Country.field :foo + country = Country.new(:foo => :bar) + expect(country._read_attribute('foo')).to eq(:bar) + end + it "works with read_attribute" do Country.field :foo country = Country.new(:foo => :bar) expect(country.read_attribute(:foo)).to eq(:bar) end + it "works when string key passed to read_attribute" do + Country.field :foo + country = Country.new(:foo => :bar) + expect(country.read_attribute('foo')).to eq(:bar) + end + it "works with #[]=" do Country.field :foo country = Country.new