diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-08-03 17:26:59 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-08-12 13:10:58 -0300 |
commit | 6ed1ba472e0f649726d9a0d04056934d1afe4573 (patch) | |
tree | 230bd8d92661a0dfe633c4a8549975744f1f2243 | |
parent | 4134d7db34506887f5cb945bb4e51c53c5f67ec3 (diff) | |
download | rails-6ed1ba472e0f649726d9a0d04056934d1afe4573.tar.gz rails-6ed1ba472e0f649726d9a0d04056934d1afe4573.tar.bz2 rails-6ed1ba472e0f649726d9a0d04056934d1afe4573.zip |
Ensure we can nest include calls [#5285 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/spawn_methods.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 12 | ||||
-rw-r--r-- | activerecord/test/models/author.rb | 3 | ||||
-rw-r--r-- | activerecord/test/models/car.rb | 5 | ||||
-rw-r--r-- | activerecord/test/models/tyre.rb | 3 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 4 |
7 files changed, 34 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index cd6c6f8d1f..0bf0b37900 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -11,7 +11,7 @@ module ActiveRecord def includes(*args) args.reject! { |a| a.blank? } - clone.tap {|r| r.includes_values += args if args.present? } + clone.tap {|r| r.includes_values = (r.includes_values + args).flatten.uniq if args.present? } end def eager_load(*args) diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index 02db8d2b89..f857e50dea 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -8,7 +8,13 @@ module ActiveRecord ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - [:joins, :where]).each do |method| value = r.send(:"#{method}_values") - merged_relation.send(:"#{method}_values=", value) if value.present? + if value.present? + if method == :includes + merged_relation = merged_relation.includes(value) + else + merged_relation.send(:"#{method}_values=", value) + end + end end merged_relation = merged_relation.joins(r.joins_values) diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index ac7b501bb7..bcc36d79be 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -10,10 +10,20 @@ require 'models/entrant' require 'models/developer' require 'models/company' require 'models/bird' +require 'models/car' +require 'models/engine' +require 'models/tyre' + class RelationTest < ActiveRecord::TestCase fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts, :comments, - :taggings + :taggings, :cars + + def test_two_named_scopes_with_includes_should_not_drop_any_include + car = Car.incl_engines.incl_tyres.first + assert_no_queries { car.tyres.length } + assert_no_queries { car.engines.length } + end def test_apply_relation_as_where_id posts = Post.arel_table diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 727978431c..34bfd2d881 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -93,6 +93,9 @@ class Author < ActiveRecord::Base belongs_to :author_address, :dependent => :destroy belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" + scope :relation_include_posts, includes(:posts) + scope :relation_include_tags, includes(:tags) + attr_accessor :post_log after_initialize :set_post_log diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb index 1101180a67..faf4e6cbc0 100644 --- a/activerecord/test/models/car.rb +++ b/activerecord/test/models/car.rb @@ -1,4 +1,9 @@ class Car < ActiveRecord::Base + has_many :tyres has_many :engines has_many :wheels, :as => :wheelable + + scope :incl_tyres, includes(:tyres) + scope :incl_engines, includes(:engines) + end diff --git a/activerecord/test/models/tyre.rb b/activerecord/test/models/tyre.rb new file mode 100644 index 0000000000..bc3444aa7d --- /dev/null +++ b/activerecord/test/models/tyre.rb @@ -0,0 +1,3 @@ +class Tyre < ActiveRecord::Base + belongs_to :car +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index fc3810f82b..c72f7b25ca 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -194,6 +194,10 @@ ActiveRecord::Schema.define do t.integer :car_id end + create_table :tyres, :force => true do |t| + t.integer :car_id + end + create_table :entrants, :force => true do |t| t.string :name, :null => false t.integer :course_id, :null => false |