aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-12-28 01:33:20 +0530
committerPratik Naik <pratiknaik@gmail.com>2009-12-28 01:33:20 +0530
commit51a1d5a6708726ae03deb5869f36e7d5cca22a6e (patch)
tree30bc57056a5f730ff89d41e715c8587fd916775f /activerecord
parenta8b10a2a8d6f8d861453803264b9ef1b0cadbc6b (diff)
downloadrails-51a1d5a6708726ae03deb5869f36e7d5cca22a6e.tar.gz
rails-51a1d5a6708726ae03deb5869f36e7d5cca22a6e.tar.bz2
rails-51a1d5a6708726ae03deb5869f36e7d5cca22a6e.zip
Handle preloads and eager loads when merging relations
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation.rb6
-rw-r--r--activerecord/test/cases/relations_test.rb20
2 files changed, 22 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 967e429fc3..d0f6c5a63f 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -2,7 +2,7 @@ module ActiveRecord
class Relation
delegate :to_sql, :to => :relation
delegate :length, :collect, :find, :map, :each, :to => :to_a
- attr_reader :relation, :klass
+ attr_reader :relation, :klass, :associations_to_preload, :eager_load_associations
def initialize(klass, relation, readonly = false, preload = [], eager_load = [])
@klass, @relation = klass, relation
@@ -19,7 +19,9 @@ module ActiveRecord
where(r.send(:where_clause)).
limit(r.taken).
offset(r.skipped).
- select(r.send(:select_clauses).join(', '))
+ select(r.send(:select_clauses).join(', ')).
+ eager_load(r.eager_load_associations).
+ preload(r.associations_to_preload)
end
alias :& :merge
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index c639fb978d..0644db362a 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -332,8 +332,24 @@ class RelationTest < ActiveRecord::TestCase
devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3")
assert_equal [developers(:david), developers(:jamis)], devs.to_a
- dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*, count(id) id_count').group('id')
+ dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*').group('id')
assert_equal [developers(:poor_jamis)], dev_with_count.to_a
- assert_equal 1, dev_with_count.first.id_count.to_i
+ end
+
+ def test_relation_merging_with_eager_load
+ relations = []
+ relations << (Post.order('comments.id DESC') & Post.eager_load(:last_comment) & Post.scoped)
+ relations << (Post.eager_load(:last_comment) & Post.order('comments.id DESC') & Post.scoped)
+
+ relations.each do |posts|
+ post = posts.find { |p| p.id == 1 }
+ assert_equal Post.find(1).last_comment, post.last_comment
+ end
+ end
+
+ def test_relation_merging_with_preload
+ [Post.scoped & Post.preload(:author), Post.preload(:author) & Post.scoped].each do |posts|
+ assert_queries(2) { assert posts.first.author }
+ end
end
end