diff options
author | alfa-jpn <a.nkmr.ja@gmail.com> | 2014-11-08 17:03:54 +0900 |
---|---|---|
committer | alfa-jpn <a.nkmr.ja@gmail.com> | 2014-11-08 17:03:54 +0900 |
commit | 9bd43868502f5e8a67649ffb5e766853a05714c5 (patch) | |
tree | 73822e597d3a868ea1b5d9e60bfcc60d9d70a79e /activerecord | |
parent | cb976371e43c90a995afb1224c325c7eed4fc194 (diff) | |
download | rails-9bd43868502f5e8a67649ffb5e766853a05714c5.tar.gz rails-9bd43868502f5e8a67649ffb5e766853a05714c5.tar.bz2 rails-9bd43868502f5e8a67649ffb5e766853a05714c5.zip |
default scopes should break the cache on singulur_association.
fixes #17495
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/associations/belongs_to_associations_test.rb | 29 |
2 files changed, 31 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index c360ef1b2c..c44242a0f0 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -41,7 +41,8 @@ module ActiveRecord def get_records if reflection.scope_chain.any?(&:any?) || scope.eager_loading? || - klass.current_scope + klass.current_scope || + klass.default_scopes.any? return scope.limit(1).to_a end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 25555bd75c..bcd0cf82d5 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -57,6 +57,35 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase end end + def test_default_scope_on_relations_is_not_cached + counter = 0 + + comments = Class.new(ActiveRecord::Base) { + self.table_name = 'comments' + self.inheritance_column = 'not_there' + + posts = Class.new(ActiveRecord::Base) { + self.table_name = 'posts' + self.inheritance_column = 'not_there' + + default_scope -> { + counter += 1 + where("id = :inc", :inc => counter) + } + + has_many :comments, :class => comments + } + belongs_to :post, :class => posts, :inverse_of => false + } + + assert_equal 0, counter + comment = comments.first + assert_equal 0, counter + sql = capture_sql { comment.post } + comment.reload + assert_not_equal sql, capture_sql { comment.post } + end + def test_proxy_assignment account = Account.find(1) assert_nothing_raised { account.firm = account.firm } |