Navigation Menu

Skip to content

Commit

Permalink
eagerly loaded association records should respect default_scope [#2931
Browse files Browse the repository at this point in the history
…state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
subbarao authored and josevalim committed Jul 21, 2010
1 parent 992711a commit d77c3b6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/association_preload.rb
Expand Up @@ -378,7 +378,7 @@ def find_associated_records(ids, reflection, preload_options)
:order => preload_options[:order] || options[:order]
}

reflection.klass.unscoped.apply_finder_options(find_options).to_a
reflection.klass.scoped.apply_finder_options(find_options).to_a
end


Expand Down
18 changes: 16 additions & 2 deletions activerecord/test/cases/relation_scoping_test.rb
Expand Up @@ -5,6 +5,8 @@
require 'models/project'
require 'models/comment'
require 'models/category'
require 'models/person'
require 'models/reference'

class RelationScopingTest < ActiveRecord::TestCase
fixtures :authors, :developers, :projects, :comments, :posts, :developers_projects
Expand Down Expand Up @@ -218,7 +220,7 @@ def test_nested_exclusive_scope_for_create
end

class HasManyScopingTest< ActiveRecord::TestCase
fixtures :comments, :posts
fixtures :comments, :posts, :people, :references

def setup
@welcome = Post.find(1)
Expand Down Expand Up @@ -250,6 +252,18 @@ def test_nested_scope_finder
assert_equal 'a comment...', @welcome.comments.what_are_you
end
end

def test_should_maintain_default_scope_on_associations
person = people(:michael)
magician = BadReference.find(1)
assert_equal [magician], people(:michael).bad_references
end

def test_should_maintain_default_scope_on_eager_loaded_associations
michael = Person.where(:id => people(:michael).id).includes(:bad_references).first
magician = BadReference.find(1)
assert_equal [magician], michael.bad_references
end
end

class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase
Expand Down Expand Up @@ -399,4 +413,4 @@ def test_create_attribute_overwrites_default_values
assert_equal nil, PoorDeveloperCalledJamis.create!(:salary => nil).salary
assert_equal 50000, PoorDeveloperCalledJamis.create!(:name => 'David').salary
end
end
end
1 change: 1 addition & 0 deletions activerecord/test/models/person.rb
Expand Up @@ -4,6 +4,7 @@ class Person < ActiveRecord::Base
has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments, :conditions => 'comments.id is null'

has_many :references
has_many :bad_references
has_many :jobs, :through => :references
has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true]
has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id'
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/models/reference.rb
Expand Up @@ -2,3 +2,8 @@ class Reference < ActiveRecord::Base
belongs_to :person
belongs_to :job
end

class BadReference < ActiveRecord::Base
self.table_name ='references'
default_scope :conditions => {:favourite => false }
end

0 comments on commit d77c3b6

Please sign in to comment.