Skip to content

Commit

Permalink
Ensure we can nest include calls [#5285 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Neeraj Singh authored and josevalim committed Aug 12, 2010
1 parent 4dcce5d commit 9528aa9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -11,7 +11,7 @@ module QueryMethods


def includes(*args) def includes(*args)
args.reject! { |a| a.blank? } 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 end


def eager_load(*args) def eager_load(*args)
Expand Down
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/relation/spawn_methods.rb
Expand Up @@ -8,7 +8,13 @@ def merge(r)


((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - [:joins, :where]).each do |method| ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - [:joins, :where]).each do |method|
value = r.send(:"#{method}_values") 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 end


merged_relation = merged_relation.joins(r.joins_values) merged_relation = merged_relation.joins(r.joins_values)
Expand Down
12 changes: 11 additions & 1 deletion activerecord/test/cases/relations_test.rb
Expand Up @@ -10,10 +10,20 @@
require 'models/developer' require 'models/developer'
require 'models/company' require 'models/company'
require 'models/bird' require 'models/bird'
require 'models/car'
require 'models/engine'
require 'models/tyre'



class RelationTest < ActiveRecord::TestCase class RelationTest < ActiveRecord::TestCase
fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts, :comments, 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 def test_apply_relation_as_where_id
posts = Post.arel_table posts = Post.arel_table
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/models/author.rb
Expand Up @@ -93,6 +93,9 @@ def testing_proxy_target
belongs_to :author_address, :dependent => :destroy belongs_to :author_address, :dependent => :destroy
belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" 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 attr_accessor :post_log
after_initialize :set_post_log after_initialize :set_post_log


Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/models/car.rb
@@ -1,4 +1,9 @@
class Car < ActiveRecord::Base class Car < ActiveRecord::Base
has_many :tyres
has_many :engines has_many :engines
has_many :wheels, :as => :wheelable has_many :wheels, :as => :wheelable

scope :incl_tyres, includes(:tyres)
scope :incl_engines, includes(:engines)

end end
3 changes: 3 additions & 0 deletions activerecord/test/models/tyre.rb
@@ -0,0 +1,3 @@
class Tyre < ActiveRecord::Base
belongs_to :car
end
4 changes: 4 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -194,6 +194,10 @@ def create_table(*args, &block)
t.integer :car_id t.integer :car_id
end end


create_table :tyres, :force => true do |t|
t.integer :car_id
end

create_table :entrants, :force => true do |t| create_table :entrants, :force => true do |t|
t.string :name, :null => false t.string :name, :null => false
t.integer :course_id, :null => false t.integer :course_id, :null => false
Expand Down

0 comments on commit 9528aa9

Please sign in to comment.