diff --git a/Manifest.txt b/Manifest.txt index 8419de28..3b398674 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -79,6 +79,7 @@ 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/associations/one_to_one_with_boolean_cpk_spec.rb spec/public/collection_spec.rb spec/public/migrations_spec.rb spec/public/model/relationship_spec.rb diff --git a/lib/dm-core/associations/many_to_one.rb b/lib/dm-core/associations/many_to_one.rb index 1d89630d..9768cc0b 100644 --- a/lib/dm-core/associations/many_to_one.rb +++ b/lib/dm-core/associations/many_to_one.rb @@ -145,7 +145,7 @@ def initialize(name, source_model, target_model, options = {}) # # @api private def lazy_load(source) - return unless source_key.get(source).all? + return unless source_key.get(source).all? { |value| !value.nil? } # SEL: load all related resources in the source collection if source.saved? && source.collection.size > 1 diff --git a/lib/dm-core/associations/one_to_one.rb b/lib/dm-core/associations/one_to_one.rb index 05f1f6b3..2fbf8d2f 100644 --- a/lib/dm-core/associations/one_to_one.rb +++ b/lib/dm-core/associations/one_to_one.rb @@ -15,7 +15,7 @@ class Relationship < Associations::Relationship def get(source, other_query = nil) assert_kind_of 'source', source, source_model - return unless loaded?(source) || source_key.get(source).all? + return unless loaded?(source) || source_key.get(source).all? { |value| !value.nil? } relationship.get(source, other_query).first end diff --git a/lib/dm-core/resource.rb b/lib/dm-core/resource.rb index 21d7cb33..d1a4b605 100644 --- a/lib/dm-core/resource.rb +++ b/lib/dm-core/resource.rb @@ -90,7 +90,7 @@ def key original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil) end - return unless key.all? + return unless key.all? { |value| !value.nil? } # memoize the key if the Resource is not frozen @key = key unless frozen? diff --git a/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb b/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb new file mode 100644 index 00000000..11f192b1 --- /dev/null +++ b/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb @@ -0,0 +1,45 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) + +# TODO: combine this into one_to_one_spec.rb + +describe 'One to One Associations when foreign key is part of a composite key and contains a boolean, with an integer and a boolean making up the composite key' do + before :all do + class ::ParentModel + include DataMapper::Resource + + property :integer_key, Integer, :key => true + property :boolean_key, Boolean, :key => true + + has 1, :child_model, :child_key => [ :integer_key, :boolean_key ] + end + + class ::ChildModel + include DataMapper::Resource + + property :integer_key, Integer, :key => true + property :other_integer_key, Integer, :key => true + property :boolean_key, Boolean, :key => true + + belongs_to :parent_model, :child_key => [ :integer_key, :boolean_key ] + end + end + + supported_by :all do + before :all do + @parent = ParentModel.create(:integer_key => 1, :boolean_key => false) + @child = ChildModel.create(:integer_key => 1, :other_integer_key => 1, :boolean_key => false) + end + + it 'should be able to access the child' do + @parent.child_model.should == @child + end + + it 'should be able to access the parent' do + @child.parent_model.should == @parent + end + + it 'should be able to access the parent_key' do + @child.parent_model.key.should_not be_nil + end + end +end