Skip to content

Commit

Permalink
Merge pull request #10713 from senny/10693_fix_primary_key_option_on_…
Browse files Browse the repository at this point in the history
…has_many

Fix the `:primary_key` option for `has_many` associations.
  • Loading branch information
rafaelfranca committed May 23, 2013
2 parents 9e5b8e3 + ef99c11 commit 030d30d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
* 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.
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/person.rb
Expand Up @@ -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') }
Expand Down

0 comments on commit 030d30d

Please sign in to comment.