mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
default scopes should break the cache on singulur_association.
fixes #17495
This commit is contained in:
parent
cb976371e4
commit
9bd4386850
2 changed files with 31 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in a new issue