Made eager loading work even more

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1083 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-04-03 17:50:11 +00:00
parent 46f2b03eac
commit f8783abf0c
11 changed files with 76 additions and 50 deletions

View File

@ -551,6 +551,14 @@ module ActiveRecord
end end
association association
end end
define_method("set_#{association_name}_target") do |target|
association = association_proxy_class.new(self,
association_name, association_class_name,
association_class_primary_key_name, options)
association.target = target
instance_variable_set("@#{association_name}", association)
end
end end
def collection_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, association_proxy_class) def collection_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, association_proxy_class)
@ -630,7 +638,7 @@ module ActiveRecord
when :has_many when :has_many
record.send(reflection.name).target = extract_association_for_record(record, rows, reflection) record.send(reflection.name).target = extract_association_for_record(record, rows, reflection)
when :has_one, :belongs_to when :has_one, :belongs_to
record.send("#{reflection.name}=", extract_association_for_record(record, rows, reflection).first) record.send("set_#{reflection.name}_target", extract_association_for_record(record, rows, reflection).first)
end end
end end
end end
@ -644,8 +652,14 @@ module ActiveRecord
sql << " FROM #{table_name} " sql << " FROM #{table_name} "
reflections.each do |reflection| reflections.each do |reflection|
sql << " LEFT JOIN #{reflection.klass.table_name} ON " + case reflection.macro
"#{reflection.klass.table_name}.#{table_name.classify.foreign_key} = #{table_name}.#{primary_key} " when :has_many, :has_one
sql << " LEFT JOIN #{reflection.klass.table_name} ON " +
"#{reflection.klass.table_name}.#{table_name.classify.foreign_key} = #{table_name}.#{primary_key} "
when :belongs_to
sql << " LEFT JOIN #{reflection.klass.table_name} ON " +
"#{reflection.klass.table_name}.#{reflection.klass.primary_key} = #{table_name}.#{reflection.klass.table_name.classify.foreign_key} "
end
end end
sql << "#{options[:joins]} " if options[:joins] sql << "#{options[:joins]} " if options[:joins]

View File

@ -50,17 +50,13 @@ module ActiveRecord
end end
def find(*args) def find(*args)
# Return an Array if multiple ids are given. options = Base.send(:extract_options_from_args!, args)
expects_array = args.first.kind_of?(Array)
ids = args.flatten.compact.uniq
# If no ids given, raise RecordNotFound.
if ids.empty?
raise RecordNotFound, "Couldn't find #{@association_class.name} without an ID"
# If using a custom finder_sql, scan the entire collection. # If using a custom finder_sql, scan the entire collection.
elsif @options[:finder_sql] if @options[:finder_sql]
expects_array = args.first.kind_of?(Array)
ids = args.flatten.compact.uniq
if ids.size == 1 if ids.size == 1
id = ids.first id = ids.first
record = load_target.detect { |record| id == record.id } record = load_target.detect { |record| id == record.id }
@ -68,11 +64,11 @@ module ActiveRecord
else else
load_target.select { |record| ids.include?(record.id) } load_target.select { |record| ids.include?(record.id) }
end end
# Otherwise, delegate to association class with conditions.
else else
args << { :conditions => "#{@association_class_primary_key_name} = #{@owner.quoted_id} #{@conditions ? " AND " + @conditions : ""}" } original_conditions = options[:conditions] ? " AND #{options[:conditions]}" : ""
@association_class.find(*args) options[:conditions] =
"#{@association_class_primary_key_name} = #{@owner.quoted_id} #{@conditions ? " AND " + @conditions : ""}#{original_conditions}"
@association_class.find(args.size == 1 ? args.first : args, options)
end end
end end

View File

@ -38,7 +38,7 @@ module ActiveRecord
private private
def find_target def find_target
@association_class.find_first(@finder_sql, @options[:order]) @association_class.find(:first, :conditions => @finder_sql, :order => @options[:order])
end end
def target_obsolete? def target_obsolete?

View File

@ -185,6 +185,9 @@ class Fixtures < Hash
DEFAULT_FILTER_RE = /\.ya?ml$/ DEFAULT_FILTER_RE = /\.ya?ml$/
def self.instantiate_fixtures(object, table_name, fixtures, load_instances=true) def self.instantiate_fixtures(object, table_name, fixtures, load_instances=true)
old_logger_level = ActiveRecord::Base.logger.level
ActiveRecord::Base.logger.level = Logger::ERROR
object.instance_variable_set "@#{table_name}", fixtures object.instance_variable_set "@#{table_name}", fixtures
if load_instances if load_instances
fixtures.each do |name, fixture| fixtures.each do |name, fixture|
@ -193,12 +196,14 @@ class Fixtures < Hash
end end
end end
end end
ActiveRecord::Base.logger.level = old_logger_level
end end
def self.instantiate_all_loaded_fixtures(object, load_instances=true) def self.instantiate_all_loaded_fixtures(object, load_instances=true)
all_loaded_fixtures.each do |table_name, fixtures| all_loaded_fixtures.each do |table_name, fixtures|
Fixtures.instantiate_fixtures(object, table_name, fixtures, load_instances) Fixtures.instantiate_fixtures(object, table_name, fixtures, load_instances)
end end
end end
cattr_accessor :all_loaded_fixtures cattr_accessor :all_loaded_fixtures

View File

@ -0,0 +1,36 @@
require 'abstract_unit'
require 'fixtures/post'
require 'fixtures/comment'
require 'fixtures/author'
class EagerAssociationTest < Test::Unit::TestCase
fixtures :posts, :comments, :authors
def test_loading_with_one_association
posts = Post.find(:all, :include => :comments)
assert_equal 2, posts.first.comments.size
assert_equal @greetings.body, posts.first.comments.first.body
post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
assert_equal 2, post.comments.size
assert_equal @greetings.body, post.comments.first.body
end
def test_loading_with_multiple_associations
posts = Post.find(:all, :include => [ :comments, :author ])
assert_equal 2, posts.first.comments.size
assert_equal @greetings.body, posts.first.comments.first.body
end
def test_loading_from_an_association
posts = @david.posts.find(:all, :include => :comments)
assert_equal 2, posts.first.comments.size
end
def test_eager_association_loading_with_belongs_to
comments = Comment.find(:all, :include => :post)
assert_equal @welcome.title, comments.first.post.title
assert_equal @thinking.title, comments.last.post.title
end
end

View File

@ -5,9 +5,6 @@ require 'fixtures/company'
require 'fixtures/topic' require 'fixtures/topic'
require 'fixtures/reply' require 'fixtures/reply'
require 'fixtures/computer' require 'fixtures/computer'
require 'fixtures/post'
require 'fixtures/comment'
require 'fixtures/author'
# Can't declare new classes in test case methods, so tests before that # Can't declare new classes in test case methods, so tests before that
bad_collection_keys = false bad_collection_keys = false
@ -206,7 +203,7 @@ end
class HasManyAssociationsTest < Test::Unit::TestCase class HasManyAssociationsTest < Test::Unit::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects, :topics, :posts, :comments fixtures :accounts, :companies, :developers, :projects, :developers_projects, :topics
def setup def setup
@signals37 = Firm.find(1) @signals37 = Firm.find(1)
@ -534,28 +531,6 @@ class HasManyAssociationsTest < Test::Unit::TestCase
def test_adding_array_and_collection def test_adding_array_and_collection
assert_nothing_raised { Firm.find_first.clients + Firm.find_all.last.clients } assert_nothing_raised { Firm.find_first.clients + Firm.find_all.last.clients }
end end
def test_eager_association_loading_with_one_association
posts = Post.find(:all, :include => :comments)
assert_equal 2, posts.first.comments.size
assert_equal @greetings.body, posts.first.comments.first.body
post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
assert_equal 2, post.comments.size
assert_equal @greetings.body, post.comments.first.body
end
def test_eager_association_loading_with_multiple_associations
posts = Post.find(:all, :include => [ :comments, :author ])
assert_equal 2, posts.first.comments.size
assert_equal @greetings.body, posts.first.comments.first.body
end
def xtest_eager_association_loading_with_belongs_to
comments = Comment.find(:all, :include => :post)
assert_equal @welcome.title, comments.first.post.title
assert_equal @thinking.title, comments.last.post.title
end
end end
class BelongsToAssociationsTest < Test::Unit::TestCase class BelongsToAssociationsTest < Test::Unit::TestCase

View File

@ -1,3 +1,3 @@
class Author < ActiveRecord::Base class Author < ActiveRecord::Base
belongs_to :post has_many :posts
end end

View File

@ -1,9 +1,7 @@
david: david:
id: 1 id: 1
post_id: 1
name: David name: David
mary: mary:
id: 2 id: 2
post_id: 2
name: Mary name: Mary

View File

@ -118,6 +118,7 @@ CREATE TABLE 'computers' (
CREATE TABLE 'posts' ( CREATE TABLE 'posts' (
'id' INTEGER NOT NULL PRIMARY KEY, 'id' INTEGER NOT NULL PRIMARY KEY,
'author_id' INTEGER NOT NULL,
'title' VARCHAR(255) NOT NULL, 'title' VARCHAR(255) NOT NULL,
'body' TEXT NOT NULL 'body' TEXT NOT NULL
); );
@ -130,7 +131,6 @@ CREATE TABLE 'comments' (
CREATE TABLE 'authors' ( CREATE TABLE 'authors' (
'id' INTEGER NOT NULL PRIMARY KEY, 'id' INTEGER NOT NULL PRIMARY KEY,
'post_id' INTEGER NOT NULL,
'name' VARCHAR(255) NOT NULL 'name' VARCHAR(255) NOT NULL
); );

View File

@ -1,4 +1,4 @@
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
has_many :comments belongs_to :author
has_one :author has_many :comments
end end

View File

@ -1,9 +1,11 @@
welcome: welcome:
id: 1 id: 1
author_id: 1
title: Welcome to the weblog title: Welcome to the weblog
body: Such a lovely day body: Such a lovely day
thinking: thinking:
id: 2 id: 2
author_id: 1
title: So I was thinking title: So I was thinking
body: Like I hopefully always am body: Like I hopefully always am