Skip to content

Instantly share code, notes, and snippets.

@JuanitoFatas
Last active August 29, 2015 14:02
Show Gist options
  • Save JuanitoFatas/ca7d5288af7b9926bb56 to your computer and use it in GitHub Desktop.
Save JuanitoFatas/ca7d5288af7b9926bb56 to your computer and use it in GitHub Desktop.
rails issue 15258
From ebb192badba2f0bf4ec476dc8c3c9ea95dbb6b00 Mon Sep 17 00:00:00 2001
From: Juanito Fatas <[email protected]>
Date: Thu, 12 Jun 2014 21:15:03 +0800
Subject: [PATCH] Add test for #15258.
---
activerecord/test/cases/relations_test.rb | 8 ++++++++
activerecord/test/models/comment.rb | 4 ++++
activerecord/test/models/post.rb | 16 ++++++++++++++++
3 files changed, 28 insertions(+)
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 88df997..afea607 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1383,6 +1383,14 @@ class RelationTest < ActiveRecord::TestCase
assert_raises(ArgumentError) { Comment.update_all({}) }
end
+ def test_update_all_with_after_save_hook
+ post = PostThatUpdateCommentBodyInAfterSaveHook.create!
+ comment = Comment15258.create!
+ post.comments << comment
+ post.update
+ assert_equal post.comments.first.body, 'foo'
+ end
+
def test_update_all_with_joins
comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id)
count = comments.count
diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb
index 1597075..be770f3 100644
--- a/activerecord/test/models/comment.rb
+++ b/activerecord/test/models/comment.rb
@@ -51,3 +51,7 @@ class CommentThatAutomaticallyAltersPostBody < Comment
comment.post.update_attributes(body: "Automatically altered")
end
end
+
+class Comment15258 < ActiveRecord::Base
+ belongs_to :post, inverse_of: :comments
+end
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 5f01ab0..28ea8d7 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -217,3 +217,19 @@ class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base
post.comments.load
end
end
+
+class PostThatUpdateCommentBodyInAfterSaveHook < ActiveRecord::Base
+ self.table_name = 'posts'
+ has_many :comments, inverse_of: :post
+ validate :given_validation
+
+ after_save :update_comments
+
+ def given_validation
+ comments.select(:post_id) # This is the line that makes the test fail.
+ end
+
+ def update_comments
+ comments.update_all(body: "foo")
+ end
+end
--
2.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment