mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure we can nest include calls [#5285 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
4134d7db34
commit
6ed1ba472e
7 changed files with 34 additions and 3 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
3
activerecord/test/models/tyre.rb
Normal file
3
activerecord/test/models/tyre.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Tyre < ActiveRecord::Base
|
||||
belongs_to :car
|
||||
end
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue