diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2012-08-28 12:48:59 -0700 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2012-08-28 12:49:55 -0700 |
commit | 754173c5ff89bf3251dfcc6577e4b3c16de8aeef (patch) | |
tree | 38441a860b917c87422077235fb150f532e74944 /activerecord/test | |
parent | 810a50dacf9ddddc1d59c1cb350e8ce785c8bf85 (diff) | |
download | rails-754173c5ff89bf3251dfcc6577e4b3c16de8aeef.tar.gz rails-754173c5ff89bf3251dfcc6577e4b3c16de8aeef.tar.bz2 rails-754173c5ff89bf3251dfcc6577e4b3c16de8aeef.zip |
Add a test to make sure preloading properly merges association and default scope conditions
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 12 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 3 | ||||
-rw-r--r-- | activerecord/test/models/reader.rb | 8 |
3 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index da4eeb3844..124bf65d3a 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -980,6 +980,18 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal posts(:welcome, :thinking), posts end + def test_preload_has_many_with_association_condition_and_default_scope + post = Post.create!(:title => 'Beaches', :body => "I like beaches!") + Reader.create! :person => people(:david), :post => post + LazyReader.create! :person => people(:susan), :post => post + + assert_equal 1, post.lazy_readers.to_a.size + assert_equal 2, post.lazy_readers_skimmers_or_not.to_a.size + + post_with_readers = Post.includes(:lazy_readers_skimmers_or_not).find(post.id) + assert_equal 2, post_with_readers.lazy_readers_skimmers_or_not.to_a.size + end + def test_eager_loading_with_conditions_on_string_joined_table_preloads posts = assert_queries(2) do Post.all.merge!(:select => 'distinct posts.*', :includes => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :where => "comments.body like 'Thank you%'", :order => 'posts.id').to_a diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 9c5b7310ff..c995f59a15 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -119,6 +119,9 @@ class Post < ActiveRecord::Base has_many :skimmers, -> { where :skimmer => true }, :class_name => 'Reader' has_many :impatient_people, :through => :skimmers, :source => :person + has_many :lazy_readers + has_many :lazy_readers_skimmers_or_not, -> { where(skimmer: [ true, false ]) }, :class_name => 'LazyReader' + def self.top(limit) ranked_by_comments.limit_by(limit) end diff --git a/activerecord/test/models/reader.rb b/activerecord/test/models/reader.rb index 59005ac604..f5b6079bd2 100644 --- a/activerecord/test/models/reader.rb +++ b/activerecord/test/models/reader.rb @@ -12,3 +12,11 @@ class SecureReader < ActiveRecord::Base attr_accessible nil end + +class LazyReader < ActiveRecord::Base + self.table_name = "readers" + default_scope -> { where(skimmer: true) } + + belongs_to :post + belongs_to :person +end |