Permalink
Browse files

Merge pull request #10713 from senny/10693_fix_primary_key_option_on_…

…has_many

Fix the `:primary_key` option for `has_many` associations.
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
1 parent 73cb3cf commit d87ee0a6fc35c80697e1ba1ea8d441bc1dc3fc79 @rafaelfranca rafaelfranca committed May 23, 2013
@@ -1,5 +1,10 @@
## unreleased ##
+* Fix the `:primary_key` option for `has_many` associations.
+ Fixes #10693.
+
+ *Yves Senn*
+
* Fix bug where tiny types are incorectly coerced as booleand when the length is more than 1.
Fixes #10620.
@@ -115,8 +115,7 @@ def delete_records(records, method)
if records == :all
scope = self.scope
else
- keys = records.map { |r| r[reflection.association_primary_key] }
- scope = self.scope.where(reflection.association_primary_key => keys)
+ scope = self.scope.where(reflection.klass.primary_key => records)
end
if method == :delete_all
@@ -1472,6 +1472,14 @@ def test_has_many_custom_primary_key
assert_equal david.essays, Essay.where(writer_id: "David")
end
+ def test_has_many_assignment_with_custom_primary_key
+ david = people(:david)
+
+ assert_equal ["A Modest Proposal"], david.essays.map(&:name)
+ david.essays = [Essay.create!(name: "Remote Work" )]
+ assert_equal ["Remote Work"], david.essays.map(&:name)
+ end
+
def test_blank_custom_primary_key_on_new_record_should_not_run_queries
author = Author.new
assert !author.essays.loaded?
@@ -32,6 +32,7 @@ class Person < ActiveRecord::Base
has_many :agents_posts, :through => :agents, :source => :posts
has_many :agents_posts_authors, :through => :agents_posts, :source => :author
+ has_many :essays, primary_key: "first_name", foreign_key: "writer_id"
scope :males, -> { where(:gender => 'M') }
scope :females, -> { where(:gender => 'F') }

0 comments on commit d87ee0a

Please sign in to comment.