From 9b2dc06d9752d7dae2af424e58e44728aa9bd3f2 Mon Sep 17 00:00:00 2001 From: Philip Hallstrom Date: Mon, 18 Aug 2008 22:35:45 -0700 Subject: [PATCH] fixed --- activerecord/lib/active_record/associations.rb | 2 +- .../associations/association_proxy.rb | 5 ++- .../associations/belongs_to_association.rb | 7 ++++- .../associations/belongs_to_associations_test.rb | 27 ++++++++++++++++++++ activerecord/test/models/company.rb | 2 + 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index b72fdb3..07e13b4 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1539,7 +1539,7 @@ module ActiveRecord def create_belongs_to_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :foreign_type, :remote, :select, :conditions, :include, :dependent, + :class_name, :foreign_key, :primary_key, :foreign_type, :remote, :select, :conditions, :include, :dependent, :counter_cache, :extend, :polymorphic, :readonly, :validate, :accessible ) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 981be3b..0cbb3ae 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -140,11 +140,12 @@ module ActiveRecord end def set_belongs_to_association_for(record) + owner_id = @reflection.options.has_key?(:primary_key) ? @owner.send(@reflection.options[:primary_key]) : @owner.id if @reflection.options[:as] - record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record? + record["#{@reflection.options[:as]}_id"] = owner_id unless @owner.new_record? record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s else - record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? + record[@reflection.primary_key_name] = owner_id unless @owner.new_record? end end diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 7c28cbd..d6c8047 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -41,7 +41,12 @@ module ActiveRecord private def find_target - @reflection.klass.find( + find_method = if @reflection.options[:primary_key] + "find_by_#{@reflection.options[:primary_key]}" + else + "find" + end + @reflection.klass.send(find_method, @owner[@reflection.primary_key_name], :select => @reflection.options[:select], :conditions => conditions, diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 9c718c4..345a62c 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -25,6 +25,33 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert !Client.find(3).firm.nil?, "Microsoft should have a firm" end + def test_belongs_to_with_primary_key + firm = Firm.find(:first) + client = firm.clients_using_primary_key.first + assert client.firm_with_primary_key === firm + end + + def test_creating_the_belonging_object_with_primary_key + firm = Firm.find(:first) + client = firm.clients_using_primary_key.create("name" => "Primary Key Client") + assert_equal client.firm_with_primary_key, firm + client.save + client.reload + assert_equal client.firm_with_primary_key, firm + assert_equal client.firm_name, firm.name + end + + def test_building_the_belonging_object_with_primary_key + firm = Firm.find(:first) + client = firm.clients_using_primary_key.build("name" => "Primary Key Client") + assert_equal client.firm_with_primary_key, firm + client.save + client.reload + assert_equal client.firm_with_primary_key, firm + assert_equal client.firm_name, firm.name + end + + def test_proxy_assignment account = Account.find(1) assert_nothing_raised { account.firm = account.firm } diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index cd43594..9dc03a5 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -81,6 +81,8 @@ class Client < Company belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of" belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1] belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true + belongs_to :firm_with_primary_key, :class_name => "Firm", :primary_key => "name", :foreign_key => "firm_name" + # Record destruction so we can test whether firm.clients.clear has # is calling client.destroy, deleting from the database, or setting -- 1.5.6.2