aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2012-08-28 12:57:41 -0700
committerPratik Naik <pratiknaik@gmail.com>2012-08-28 12:57:41 -0700
commit58d35f62110d4280818d49f6a76b3bdaa1ee217a (patch)
tree786f6d111464f7e499c0283be1267a2b091fcde1
parent8efced68be645af03e0b49d3a12d85b8d1aaee7a (diff)
downloadrails-58d35f62110d4280818d49f6a76b3bdaa1ee217a.tar.gz
rails-58d35f62110d4280818d49f6a76b3bdaa1ee217a.tar.bz2
rails-58d35f62110d4280818d49f6a76b3bdaa1ee217a.zip
Ensure association preloading properly merges default scope and association conditions
Conflicts: activerecord/test/models/reader.rb
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb4
-rw-r--r--activerecord/test/cases/associations/eager_test.rb12
-rw-r--r--activerecord/test/models/post.rb4
-rw-r--r--activerecord/test/models/reader.rb8
4 files changed, 25 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb
index 779f8164cc..85b1040049 100644
--- a/activerecord/lib/active_record/associations/preloader/association.rb
+++ b/activerecord/lib/active_record/associations/preloader/association.rb
@@ -117,9 +117,7 @@ module ActiveRecord
conditions = klass.send(:instance_eval, &conditions)
end
- if conditions
- klass.send(:sanitize_sql, conditions)
- end
+ conditions
end
end
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 1dc71ac4cc..bf01b46852 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -1010,6 +1010,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
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_include_has_many_using_primary_key
expected = Firm.find(1).clients_using_primary_key.sort_by(&:name)
# Oracle adapter truncates alias to 30 characters
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 0fc22ac6a3..9aa02fa18f 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -117,6 +117,7 @@ class Post < ActiveRecord::Base
has_many :readers
has_many :secure_readers
has_many :readers_with_person, :include => :person, :class_name => "Reader"
+
has_many :people, :through => :readers
has_many :secure_people, :through => :secure_readers
has_many :single_people, :through => :readers
@@ -128,6 +129,9 @@ class Post < ActiveRecord::Base
has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true }
has_many :impatient_people, :through => :skimmers, :source => :person
+ has_many :lazy_readers
+ has_many :lazy_readers_skimmers_or_not, :conditions => { :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..9a1b539791 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