default scopes should break the cache on singulur_association.

fixes #17495
This commit is contained in:
alfa-jpn 2014-11-08 17:03:54 +09:00
parent cb976371e4
commit 9bd4386850
2 changed files with 31 additions and 1 deletions

@ -41,7 +41,8 @@ def create_scope
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 @@ def test_belongs_to_with_primary_key_joins_on_correct_column
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 }