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

Commit

Permalink
Fix m:1 with CPK containing a boolean property
Browse files Browse the repository at this point in the history
* Fix errors in a many-to-one relationship when the many side has a cpk
  consisting of an integer and a boolean, the one side constisting of an
  integer, and the fk in the many side being the integer of the cpk

[#993 state:resolved]

Signed-off-by: Dan Kubb <dan.kubb@gmail.com>
  • Loading branch information
angelic authored and dkubb committed Oct 16, 2009
1 parent f120aaa commit abe512d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ spec/lib/pending_helpers.rb
spec/lib/rspec_immediate_feedback_formatter.rb
spec/public/associations/many_to_many_spec.rb
spec/public/associations/many_to_one_spec.rb
spec/public/associations/many_to_one_with_boolean_cpk_spec.rb
spec/public/associations/one_to_many_spec.rb
spec/public/associations/one_to_one_spec.rb
spec/public/collection_spec.rb
Expand Down
4 changes: 2 additions & 2 deletions lib/dm-core/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def load(records, query)

model = discriminator && record[discriminator] || self

resource = if (key_values = record.values_at(*model.key(repository_name))).all?
resource = if (key_values = record.values_at(*model.key(repository_name))).all? { |value| !value.nil? }
identity_map = repository.identity_map(model)
identity_map[key_values]
end
Expand All @@ -499,7 +499,7 @@ def load(records, query)
when Resource
model = record.model

resource = if (key_values = record.key).all?
resource = if (key_values = record.key).all? { |value| !value.nil? }
identity_map = repository.identity_map(model)
identity_map[key_values]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/model/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def set_paranoid_property(name, &block)
# TODO: document
# @api private
def key_conditions(repository, key)
self.key(repository.name).zip(key).to_hash
self.key(repository.name).zip(key.nil? ? [] : key).to_hash
end

private
Expand Down
39 changes: 39 additions & 0 deletions spec/public/associations/many_to_one_with_boolean_cpk_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))

# TODO: combine this into many_to_one_spec.rb

describe 'Many to One Associations when foreign key is part of a composite key, with an integer and a boolean making up the composite key' do
before :all do
class ::ManyModel
include DataMapper::Resource

property :integer_key, Integer, :key => true
property :boolean_key, Boolean, :key => true

belongs_to :one_model, :child_key => [ :integer_key ]
end

class ::OneModel
include DataMapper::Resource

property :integer_key, Integer, :key => true

has n, :many_models, :child_key => [ :integer_key ]
end
end

supported_by :all do
before :all do
@one = OneModel.create(:integer_key => 1)
@many = ManyModel.create(:integer_key => 1, :boolean_key => false)
end

it 'should be able to access parent' do
@many.one_model.should == @one
end

it 'should be able to access the child' do
@one.many_models.should == [ @many ]
end
end
end

0 comments on commit abe512d

Please sign in to comment.