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

Commit

Permalink
Make sure m:m joins join with the FK of the inner association properly
Browse files Browse the repository at this point in the history
[#903 state:resolved]
  • Loading branch information
dkubb committed Feb 1, 2010
1 parent 7e627c4 commit f7aef8a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/dm-core/query/conditions/comparison.rb
Expand Up @@ -471,8 +471,10 @@ def matches?(record)
#
# @api semipublic
def foreign_key_mapping
inverse = subject.inverse
Query.target_conditions(value, inverse.source_key, inverse.target_key)
relationship = subject.inverse
relationship = relationship.links.first if relationship.respond_to?(:links)

Query.target_conditions(value, relationship.source_key, relationship.target_key)
end

private
Expand Down
67 changes: 67 additions & 0 deletions spec/public/associations/many_to_many/read_multiple_join_spec.rb
@@ -0,0 +1,67 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper'))

describe 'Many to Many Associations read across multiple join associations' do
before :all do
class User
include DataMapper::Resource

property :id, Serial

has n, :sales
has n, :sale_items, :through => :sales
has n, :items, :through => :sale_items
end

class Sale
include DataMapper::Resource

property :id, Serial

belongs_to :user
has n, :sale_items
has n, :items, :through => :sale_items
end

class SaleItem
include DataMapper::Resource

property :id, Serial

belongs_to :sale
belongs_to :item
end

class Item
include DataMapper::Resource

property :id, Serial

has n, :sale_items
end
end

supported_by :all do
before :all do
@user = User.create
@sale = @user.sales.create

5.times { @sale.items.create }
end

before :all do
@no_join = defined?(DataMapper::Adapters::InMemoryAdapter) && @adapter.kind_of?(DataMapper::Adapters::InMemoryAdapter) ||
defined?(DataMapper::Adapters::YamlAdapter) && @adapter.kind_of?(DataMapper::Adapters::YamlAdapter)

@skip = @no_join
end

before do
pending if @skip
end

it 'should return all the created entries' do
@user.items.to_a.should == Item.all.to_a
@sale.items.to_a.should == Item.all.to_a
end
end
end

2 comments on commit f7aef8a

@tpitale
Copy link
Member

@tpitale tpitale commented on f7aef8a Feb 1, 2010

Choose a reason for hiding this comment

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

Rock!

@tj
Copy link

@tj tj commented on f7aef8a Feb 1, 2010

Choose a reason for hiding this comment

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

sweet, i think this was a problem i had a week or so ago

Please sign in to comment.