Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ensure we can nest include calls [#5285 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 9528aa9f865cb22f3fec21dbaa9f3a434292af4e 1 parent 4dcce5d
@neerajdotname neerajdotname authored josevalim committed
View
2  activerecord/lib/active_record/relation/query_methods.rb
@@ -11,7 +11,7 @@ module QueryMethods
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)
View
8 activerecord/lib/active_record/relation/spawn_methods.rb
@@ -8,7 +8,13 @@ def merge(r)
((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)
View
12 activerecord/test/cases/relations_test.rb
@@ -10,10 +10,20 @@
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
View
3  activerecord/test/models/author.rb
@@ -93,6 +93,9 @@ def testing_proxy_target
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
View
5 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
View
3  activerecord/test/models/tyre.rb
@@ -0,0 +1,3 @@
+class Tyre < ActiveRecord::Base
+ belongs_to :car
+end
View
4 activerecord/test/schema/schema.rb
@@ -194,6 +194,10 @@ def create_table(*args, &block)
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
Please sign in to comment.
Something went wrong with that request. Please try again.