aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-05-21 17:59:37 +0200
committerYves Senn <yves.senn@gmail.com>2013-05-23 07:38:33 +0200
commitef99c1147592e91bb256952986470592ea0e5f6c (patch)
tree50f10c0b8e4d40314f0324b5286400e1f608a6fb /activerecord/test
parent30d28b19584783218e842ce2fd7bfe2bc1dccf66 (diff)
downloadrails-ef99c1147592e91bb256952986470592ea0e5f6c.tar.gz
rails-ef99c1147592e91bb256952986470592ea0e5f6c.tar.bz2
rails-ef99c1147592e91bb256952986470592ea0e5f6c.zip
Fix the `:primary_key` option for `has_many` associations.
When removing records from a `has_many` association it used the `primary_key` defined on the association. Our test suite didn't fail because on all occurences of `:primary_key`, the specified column was available in both tables. This prevented the code from raising an exception but it still behaved badly. I added a test-case to prevent regressions that failed with: ``` 1) Error: HasManyAssociationsTest#test_has_many_assignment_with_custom_primary_key: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: essays.first_name: UPDATE "essays" SET "writer_id" = NULL WHERE "essays"."writer_id" = ? AND "essays"."first_name" IS NULL ```
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 d85570236f..058c812dca 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1472,6 +1472,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
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?
diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb
index 2985160d28..1a282dbce4 100644
--- a/activerecord/test/models/person.rb
+++ b/activerecord/test/models/person.rb
@@ -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') }