mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
make sure cache is not used for collection assocations too
follow up for #17052
This commit is contained in:
parent
b59d47d82e
commit
2eee8af4d0
3 changed files with 19 additions and 2 deletions
|
@ -407,7 +407,12 @@ module ActiveRecord
|
|||
|
||||
private
|
||||
def get_records
|
||||
return scope.to_a if reflection.scope_chain.any?(&:any?) || scope.eager_loading?
|
||||
if reflection.scope_chain.any?(&:any?) ||
|
||||
scope.eager_loading? ||
|
||||
klass.current_scope
|
||||
|
||||
return scope.to_a
|
||||
end
|
||||
|
||||
conn = klass.connection
|
||||
sc = reflection.association_scope_cache(conn, owner) do
|
||||
|
|
|
@ -7,6 +7,7 @@ require 'models/comment'
|
|||
require 'models/category'
|
||||
require 'models/person'
|
||||
require 'models/reference'
|
||||
require 'models/reference'
|
||||
|
||||
class RelationScopingTest < ActiveRecord::TestCase
|
||||
fixtures :authors, :developers, :projects, :comments, :posts, :developers_projects
|
||||
|
@ -19,12 +20,22 @@ class RelationScopingTest < ActiveRecord::TestCase
|
|||
author = authors :mary
|
||||
assert_nil author.first_post
|
||||
post = FirstPost.unscoped do
|
||||
author = authors :mary
|
||||
author.reload.first_post
|
||||
end
|
||||
assert post
|
||||
end
|
||||
|
||||
def test_scope_breaks_caching_on_collections
|
||||
author = authors :david
|
||||
ids = author.reload.special_posts_with_default_scope.map(&:id)
|
||||
assert_equal [1,5,6], ids.sort
|
||||
scoped_posts = SpecialPostWithDefaultScope.unscoped do
|
||||
author = authors :david
|
||||
author.reload.special_posts_with_default_scope.to_a
|
||||
end
|
||||
assert_equal author.posts.map(&:id).sort, scoped_posts.map(&:id).sort
|
||||
end
|
||||
|
||||
def test_reverse_order
|
||||
assert_equal Developer.order("id DESC").to_a.reverse, Developer.order("id DESC").reverse_order
|
||||
end
|
||||
|
|
|
@ -44,6 +44,7 @@ class Author < ActiveRecord::Base
|
|||
|
||||
has_many :special_posts
|
||||
has_many :special_post_comments, :through => :special_posts, :source => :comments
|
||||
has_many :special_posts_with_default_scope, :class_name => 'SpecialPostWithDefaultScope'
|
||||
|
||||
has_many :sti_posts, :class_name => 'StiPost'
|
||||
has_many :sti_post_comments, :through => :sti_posts, :source => :comments
|
||||
|
|
Loading…
Reference in a new issue