public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix HasManyAssociation#create ignoring the :primary_key option [#1633 
state:resolved]

Signed-off-by: Frederick Cheung <frederick.cheung@gmail.com>
Roman Shterenzon (author)
Fri Dec 26 17:10:29 -0800 2008
fcheung (committer)
Fri Dec 26 17:10:29 -0800 2008
commit  21efba464afa2ae6e5dfd938ac8a3ce446faf7e7
tree    0d4958fdb36d32a94c3c7295bf4cb129ebcc7507
parent  f9cab0e503a4721c9d0369f89bb85c6e658f778c
...
180
181
182
183
 
 
 
 
184
185
186
...
180
181
182
 
183
184
185
186
187
188
189
0
@@ -180,7 +180,10 @@ module ActiveRecord
0
             record["#{@reflection.options[:as]}_id"]   = @owner.id unless @owner.new_record?
0
             record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
0
           else
0
-            record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
0
+            unless @owner.new_record?
0
+              primary_key = @reflection.options[:primary_key] || :id
0
+              record[@reflection.primary_key_name] = @owner.send(primary_key)
0
+            end
0
           end
0
         end
0
 
...
1115
1116
1117
 
 
 
 
 
 
1118
1119
...
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
0
@@ -1115,5 +1115,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert !client_association.respond_to?(:private_method)
0
     assert client_association.respond_to?(:private_method, true)
0
   end
0
+
0
+  def test_creating_using_primary_key
0
+    firm = Firm.find(:first)
0
+    client = firm.clients_using_primary_key.create!(:name => 'test')
0
+    assert_equal firm.name, client.firm_name
0
+  end
0
 end
0
 

Comments