aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-05-23 06:58:10 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-05-23 18:45:12 -0300
commitd29399061e54092fdbf57780c38e19dd1921f45d (patch)
tree9f7622ff464de62454531ba6fcb329e34450fc4e /activerecord/test
parentf8c4805a8247a62594d29c5f3ef634d57d2abbf1 (diff)
downloadrails-d29399061e54092fdbf57780c38e19dd1921f45d.tar.gz
rails-d29399061e54092fdbf57780c38e19dd1921f45d.tar.bz2
rails-d29399061e54092fdbf57780c38e19dd1921f45d.zip
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 activerecord/lib/active_record/associations/has_many_association.rb
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb8
-rw-r--r--activerecord/test/models/person.rb1
2 files changed, 9 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 869ec1e4b8..8fd9de07c9 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1492,6 +1492,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal david.essays, Essay.find_all_by_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?
diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb
index 07c529d685..3a7a730a1a 100644
--- a/activerecord/test/models/person.rb
+++ b/activerecord/test/models/person.rb
@@ -27,6 +27,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, :conditions => { :gender => 'M' }
scope :females, :conditions => { :gender => 'F' }