mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge branch 'associations_2'
This commit is contained in:
commit
1daceeb838
6 changed files with 90 additions and 13 deletions
|
@ -1458,9 +1458,9 @@ module ActiveRecord
|
||||||
after_destroy(method_name)
|
after_destroy(method_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_with_associations(options = {})
|
def find_with_associations(options = {}, join_dependency = nil)
|
||||||
catch :invalid_query do
|
catch :invalid_query do
|
||||||
join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
|
join_dependency ||= JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
|
||||||
rows = select_all_rows(options, join_dependency)
|
rows = select_all_rows(options, join_dependency)
|
||||||
return join_dependency.instantiate(rows)
|
return join_dependency.instantiate(rows)
|
||||||
end
|
end
|
||||||
|
|
|
@ -664,11 +664,23 @@ module ActiveRecord #:nodoc:
|
||||||
# This is an alias for find(:all). You can pass in all the same arguments to this method as you can
|
# This is an alias for find(:all). You can pass in all the same arguments to this method as you can
|
||||||
# to find(:all)
|
# to find(:all)
|
||||||
def all(*args)
|
def all(*args)
|
||||||
if args.empty? && !scoped?(:find)
|
options = args.extract_options!
|
||||||
arel_table
|
|
||||||
|
if options.empty? && !scoped?(:find)
|
||||||
|
relation = arel_table
|
||||||
else
|
else
|
||||||
construct_finder_arel(*args)
|
relation = construct_finder_arel(options)
|
||||||
|
include_associations = merge_includes(scope(:find, :include), options[:include])
|
||||||
|
|
||||||
|
if include_associations.any?
|
||||||
|
if references_eager_loaded_tables?(options)
|
||||||
|
relation.eager_load(include_associations)
|
||||||
|
else
|
||||||
|
relation.preload(include_associations)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
relation
|
||||||
end
|
end
|
||||||
|
|
||||||
# Executes a custom SQL query against your database and returns all the results. The results will
|
# Executes a custom SQL query against your database and returns all the results. The results will
|
||||||
|
@ -1719,7 +1731,6 @@ module ActiveRecord #:nodoc:
|
||||||
relation = relation.readonly if options[:readonly]
|
relation = relation.readonly if options[:readonly]
|
||||||
|
|
||||||
relation
|
relation
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def construct_finder_sql(options, scope = scope(:find))
|
def construct_finder_sql(options, scope = scope(:find))
|
||||||
|
|
|
@ -6,6 +6,18 @@ module ActiveRecord
|
||||||
def initialize(klass, relation)
|
def initialize(klass, relation)
|
||||||
@klass, @relation = klass, relation
|
@klass, @relation = klass, relation
|
||||||
@readonly = false
|
@readonly = false
|
||||||
|
@associations_to_preload = []
|
||||||
|
@eager_load_associations = []
|
||||||
|
end
|
||||||
|
|
||||||
|
def preload(association)
|
||||||
|
@associations_to_preload += association
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def eager_load(association)
|
||||||
|
@eager_load_associations += association
|
||||||
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def readonly
|
def readonly
|
||||||
|
@ -14,9 +26,23 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
records = @klass.find_by_sql(@relation.to_sql)
|
if @eager_load_associations.any?
|
||||||
|
records = catch :invalid_query do
|
||||||
records.each { |record| record.readonly! } if @readonly
|
@klass.send(:find_with_associations, {
|
||||||
|
:select => @relation.send(:select_clauses).join(', '),
|
||||||
|
:joins => @relation.joins(relation),
|
||||||
|
:group => @relation.send(:group_clauses).join(', '),
|
||||||
|
:order => @relation.send(:order_clauses).join(', '),
|
||||||
|
:conditions => @relation.send(:where_clauses).join("\n\tAND "),
|
||||||
|
:limit => @relation.taken
|
||||||
|
},
|
||||||
|
ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
records = @klass.find_by_sql(@relation.to_sql)
|
||||||
|
@klass.send(:preload_associations, records, @associations_to_preload) unless @associations_to_preload.empty?
|
||||||
|
records.each { |record| record.readonly! } if @readonly
|
||||||
|
end
|
||||||
|
|
||||||
records
|
records
|
||||||
end
|
end
|
||||||
|
|
|
@ -104,7 +104,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
|
||||||
authors.first.posts.first.special_comments.first.post.very_special_comment
|
authors.first.posts.first.special_comments.first.post.very_special_comment
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_eager_association_loading_where_first_level_returns_nil
|
def test_eager_association_loading_where_first_level_returns_nil
|
||||||
authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
|
authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
|
||||||
assert_equal [authors(:mary), authors(:david)], authors
|
assert_equal [authors(:mary), authors(:david)], authors
|
||||||
|
|
|
@ -593,12 +593,12 @@ class DefaultScopingTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_scope_with_conditions_string
|
def test_default_scope_with_conditions_string
|
||||||
assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.all.to_a.map(&:id).sort
|
assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.find(:all).map(&:id).sort
|
||||||
assert_equal nil, DeveloperCalledDavid.create!.name
|
assert_equal nil, DeveloperCalledDavid.create!.name
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_scope_with_conditions_hash
|
def test_default_scope_with_conditions_hash
|
||||||
assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.to_a.map(&:id).sort
|
assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.find(:all).map(&:id).sort
|
||||||
assert_equal 'Jamis', DeveloperCalledJamis.create!.name
|
assert_equal 'Jamis', DeveloperCalledJamis.create!.name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
require "cases/helper"
|
require "cases/helper"
|
||||||
require 'models/post'
|
require 'models/post'
|
||||||
require 'models/topic'
|
require 'models/topic'
|
||||||
|
require 'models/comment'
|
||||||
require 'models/reply'
|
require 'models/reply'
|
||||||
require 'models/author'
|
require 'models/author'
|
||||||
require 'models/entrant'
|
require 'models/entrant'
|
||||||
|
@ -8,7 +9,7 @@ require 'models/developer'
|
||||||
require 'models/company'
|
require 'models/company'
|
||||||
|
|
||||||
class RelationTest < ActiveRecord::TestCase
|
class RelationTest < ActiveRecord::TestCase
|
||||||
fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts
|
fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts, :comments
|
||||||
|
|
||||||
def test_finding_with_conditions
|
def test_finding_with_conditions
|
||||||
assert_equal Author.find(:all, :conditions => "name = 'David'"), Author.all.conditions("name = 'David'").to_a
|
assert_equal Author.find(:all, :conditions => "name = 'David'"), Author.all.conditions("name = 'David'").to_a
|
||||||
|
@ -85,5 +86,44 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
Developer.all.readonly.each { |d| assert d.readonly? }
|
Developer.all.readonly.each { |d| assert d.readonly? }
|
||||||
Developer.all(:readonly => true).each { |d| assert d.readonly? }
|
Developer.all(:readonly => true).each { |d| assert d.readonly? }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_eager_association_loading_of_stis_with_multiple_references
|
||||||
|
authors = Author.all(:include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4').to_a
|
||||||
|
assert_equal [authors(:david)], authors
|
||||||
|
assert_no_queries do
|
||||||
|
authors.first.posts.first.special_comments.first.post.special_comments
|
||||||
|
authors.first.posts.first.special_comments.first.post.very_special_comment
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_find_with_included_associations
|
||||||
|
assert_queries(2) do
|
||||||
|
posts = Post.find(:all, :include => :comments)
|
||||||
|
posts.first.comments.first
|
||||||
|
end
|
||||||
|
assert_queries(2) do
|
||||||
|
posts = Post.all(:include => :comments).to_a
|
||||||
|
posts.first.comments.first
|
||||||
|
end
|
||||||
|
assert_queries(2) do
|
||||||
|
posts = Post.find(:all, :include => :author)
|
||||||
|
posts.first.author
|
||||||
|
end
|
||||||
|
assert_queries(2) do
|
||||||
|
posts = Post.all(:include => :author).to_a
|
||||||
|
posts.first.author
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_default_scope_with_conditions_string
|
||||||
|
assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.all.to_a.map(&:id).sort
|
||||||
|
assert_equal nil, DeveloperCalledDavid.create!.name
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_default_scope_with_conditions_hash
|
||||||
|
assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.to_a.map(&:id).sort
|
||||||
|
assert_equal 'Jamis', DeveloperCalledJamis.create!.name
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue