Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Ensure that using id: [..] as condition returns unique records #266

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/active_hash/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down