diff --git a/lib/active_hash/relation.rb b/lib/active_hash/relation.rb index 90a7b50f..ea9384ff 100644 --- a/lib/active_hash/relation.rb +++ b/lib/active_hash/relation.rb @@ -10,7 +10,7 @@ class Relation def initialize(klass, all_records, query_hash = nil) self.klass = klass self.all_records = all_records - self.query_hash = query_hash + self.query_hash = query_hash.deep_symbolize_keys self.records_dirty = false self end @@ -130,10 +130,10 @@ def filter_all_records_by_query_hash return all_records if query_hash.blank? # use index if searching by id - if query_hash.key?(:id) || query_hash.key?("id") - ids = (query_hash.delete(:id) || query_hash.delete("id")) + if query_hash.key?(:id) + ids = query_hash.delete(:id) ids = range_to_array(ids) if ids.is_a?(Range) - candidates = Array.wrap(ids).map { |id| klass.find_by_id(id) }.compact + candidates = Array.wrap(ids).uniq.map { |id| klass.find_by_id(id) }.compact end return candidates if query_hash.blank? diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index 4c6b5063..df1d12ef 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -282,7 +282,7 @@ class Region < ActiveHash::Base end it "returns multiple records for multiple ids" do - expect(Country.where(:id => %w(1 2)).map(&:id)).to match_array([1,2]) + expect(Country.where(:id => %w(1 1 2)).map(&:id)).to match_array([1,2]) end it "returns multiple records for range argument" do