Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Fixed bug where many to one query would be unscoped
Browse files Browse the repository at this point in the history
[#881 state:resolved]
  • Loading branch information
dkubb committed Jun 4, 2009
1 parent de9da83 commit e1bd009
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/dm-core/query.rb
Expand Up @@ -977,16 +977,15 @@ def append_condition(subject, bind_value, operator = :eql)
subject

when Associations::Relationship
# TODO: handle compound keys. Consider pushing this into the adapter
source_key = subject.source_key.first
target_key = subject.target_key.first

# TODO: when the bind_value is a Collection, and it is not loaded
# then use a subquery to scope the results rather than lazy loading
# it just to retrieve the Resource key

if (resources = Array(bind_value).select { |r| r.saved? }).any?
source_values = resources.map { |r| target_key.get(r) }
# TODO: handle compound keys. Consider pushing this into the adapter
source_key = subject.source_key.first
target_key = subject.target_key.first

if (source_values = Array(bind_value).map { |resource| target_key.get!(resource) }.compact).any?
append_condition(source_key, source_values, operator)
end

Expand Down
21 changes: 21 additions & 0 deletions spec/public/model/relationship_spec.rb
Expand Up @@ -507,6 +507,27 @@ def n
end

supported_by :all do
describe 'querying for a parent resource when only the foreign key is set' do
before :all do
# create a car that would be returned if the query is not
# scoped properly to retrieve @car
Car.create

@car = Car.create
engine = Engine.new(:car_id => @car.id)

@return = engine.car
end

it 'should return a Resource' do
@return.should be_kind_of(DataMapper::Resource)
end

it 'should return expected Resource' do
@return.should eql(@car)
end
end

describe 'querying for a parent resource' do
before :all do
@car = Car.create
Expand Down

1 comment on commit e1bd009

@michael
Copy link

@michael michael commented on e1bd009 Jun 4, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for responding that quickly!
Finally transition from 0.9 to 0.10 now seems to work in my case ...

Please sign in to comment.