aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorEdgars Beigarts <edgars.beigarts@gmail.com>2016-12-02 17:09:09 +0200
committerEdgars Beigarts <edgars.beigarts@gmail.com>2016-12-02 17:09:09 +0200
commitadebb207805cb573b2387f478b4a5549badab5e9 (patch)
tree4ea72a9ddbfaf84e9828522560093c24b3551865 /activerecord
parent8ce903af862d56b77d60c3809c0442cdac9d6c89 (diff)
downloadrails-adebb207805cb573b2387f478b4a5549badab5e9.tar.gz
rails-adebb207805cb573b2387f478b4a5549badab5e9.tar.bz2
rails-adebb207805cb573b2387f478b4a5549badab5e9.zip
Reload association scope inside autosaved associations
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/autosave_association.rb6
-rw-r--r--activerecord/test/cases/autosave_association_test.rb27
2 files changed, 30 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index b343332bae..9d0b501862 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -383,6 +383,9 @@ module ActiveRecord
if association = association_instance_get(reflection.name)
autosave = reflection.options[:autosave]
+ # reconstruct the scope now that we know the owner's id
+ association.reset_scope if association.respond_to?(:reset_scope)
+
if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
if autosave
records_to_destroy = records.select(&:marked_for_destruction?)
@@ -408,9 +411,6 @@ module ActiveRecord
raise ActiveRecord::Rollback unless saved
end
end
-
- # reconstruct the scope now that we know the owner's id
- association.reset_scope if association.respond_to?(:reset_scope)
end
end
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
index a3f82ed49d..336cf9f28f 100644
--- a/activerecord/test/cases/autosave_association_test.rb
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -649,6 +649,33 @@ class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCa
assert_equal 2, firm.clients.length
assert_includes firm.clients, Client.find_by_name("New Client")
end
+
+ def test_inverse_association_within_autosave_after_save_callback
+ posts = Class.new(ActiveRecord::Base) do
+ self.table_name = "posts"
+ end
+ comments = Class.new(ActiveRecord::Base) do
+ self.table_name = "comments"
+ end
+ posts.class_eval do
+ has_many :comments, inverse_of: :post, foreign_key: :post_id, anonymous_class: comments
+ end
+ comments.class_eval do
+ belongs_to :post, inverse_of: :comments, anonymous_class: posts
+
+ attr_accessor :post_comments_count
+ after_save do
+ self.post_comments_count = post.comments.count
+ end
+ end
+
+ post = posts.new(title: "Test", body: "...")
+ comment = post.comments.build(body: "...")
+ post.save!
+
+ assert_equal 1, post.comments.count
+ assert_equal 1, comment.post_comments_count
+ end
end
class TestDefaultAutosaveAssociationOnNewRecord < ActiveRecord::TestCase