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:
parent
46f2b03eac
commit
f8783abf0c
|
@ -551,6 +551,14 @@ module ActiveRecord
|
|||
end
|
||||
association
|
||||
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
|
||||
|
||||
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
|
||||
record.send(reflection.name).target = extract_association_for_record(record, rows, reflection)
|
||||
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
|
||||
|
@ -644,8 +652,14 @@ module ActiveRecord
|
|||
sql << " FROM #{table_name} "
|
||||
|
||||
reflections.each do |reflection|
|
||||
sql << " LEFT JOIN #{reflection.klass.table_name} ON " +
|
||||
"#{reflection.klass.table_name}.#{table_name.classify.foreign_key} = #{table_name}.#{primary_key} "
|
||||
case reflection.macro
|
||||
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
|
||||
|
||||
sql << "#{options[:joins]} " if options[:joins]
|
||||
|
|
|
@ -50,17 +50,13 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def find(*args)
|
||||
# Return an Array if multiple ids are given.
|
||||
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"
|
||||
options = Base.send(:extract_options_from_args!, args)
|
||||
|
||||
# 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
|
||||
id = ids.first
|
||||
record = load_target.detect { |record| id == record.id }
|
||||
|
@ -68,11 +64,11 @@ module ActiveRecord
|
|||
else
|
||||
load_target.select { |record| ids.include?(record.id) }
|
||||
end
|
||||
|
||||
# Otherwise, delegate to association class with conditions.
|
||||
else
|
||||
args << { :conditions => "#{@association_class_primary_key_name} = #{@owner.quoted_id} #{@conditions ? " AND " + @conditions : ""}" }
|
||||
@association_class.find(*args)
|
||||
original_conditions = options[:conditions] ? " AND #{options[:conditions]}" : ""
|
||||
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
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ module ActiveRecord
|
|||
|
||||
private
|
||||
def find_target
|
||||
@association_class.find_first(@finder_sql, @options[:order])
|
||||
@association_class.find(:first, :conditions => @finder_sql, :order => @options[:order])
|
||||
end
|
||||
|
||||
def target_obsolete?
|
||||
|
|
|
@ -185,6 +185,9 @@ class Fixtures < Hash
|
|||
DEFAULT_FILTER_RE = /\.ya?ml$/
|
||||
|
||||
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
|
||||
if load_instances
|
||||
fixtures.each do |name, fixture|
|
||||
|
@ -193,12 +196,14 @@ class Fixtures < Hash
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveRecord::Base.logger.level = old_logger_level
|
||||
end
|
||||
|
||||
def self.instantiate_all_loaded_fixtures(object, load_instances=true)
|
||||
all_loaded_fixtures.each do |table_name, fixtures|
|
||||
Fixtures.instantiate_fixtures(object, table_name, fixtures, load_instances)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cattr_accessor :all_loaded_fixtures
|
||||
|
|
|
@ -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
|
||||
|
|
@ -5,9 +5,6 @@ require 'fixtures/company'
|
|||
require 'fixtures/topic'
|
||||
require 'fixtures/reply'
|
||||
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
|
||||
bad_collection_keys = false
|
||||
|
@ -206,7 +203,7 @@ end
|
|||
|
||||
|
||||
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
|
||||
@signals37 = Firm.find(1)
|
||||
|
@ -534,28 +531,6 @@ class HasManyAssociationsTest < Test::Unit::TestCase
|
|||
def test_adding_array_and_collection
|
||||
assert_nothing_raised { Firm.find_first.clients + Firm.find_all.last.clients }
|
||||
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
|
||||
|
||||
class BelongsToAssociationsTest < Test::Unit::TestCase
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
class Author < ActiveRecord::Base
|
||||
belongs_to :post
|
||||
has_many :posts
|
||||
end
|
|
@ -1,9 +1,7 @@
|
|||
david:
|
||||
id: 1
|
||||
post_id: 1
|
||||
name: David
|
||||
|
||||
mary:
|
||||
id: 2
|
||||
post_id: 2
|
||||
name: Mary
|
||||
|
|
|
@ -118,6 +118,7 @@ CREATE TABLE 'computers' (
|
|||
|
||||
CREATE TABLE 'posts' (
|
||||
'id' INTEGER NOT NULL PRIMARY KEY,
|
||||
'author_id' INTEGER NOT NULL,
|
||||
'title' VARCHAR(255) NOT NULL,
|
||||
'body' TEXT NOT NULL
|
||||
);
|
||||
|
@ -130,7 +131,6 @@ CREATE TABLE 'comments' (
|
|||
|
||||
CREATE TABLE 'authors' (
|
||||
'id' INTEGER NOT NULL PRIMARY KEY,
|
||||
'post_id' INTEGER NOT NULL,
|
||||
'name' VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class Post < ActiveRecord::Base
|
||||
has_many :comments
|
||||
has_one :author
|
||||
belongs_to :author
|
||||
has_many :comments
|
||||
end
|
|
@ -1,9 +1,11 @@
|
|||
welcome:
|
||||
id: 1
|
||||
author_id: 1
|
||||
title: Welcome to the weblog
|
||||
body: Such a lovely day
|
||||
|
||||
thinking:
|
||||
id: 2
|
||||
author_id: 1
|
||||
title: So I was thinking
|
||||
body: Like I hopefully always am
|
||||
|
|
Loading…
Reference in New Issue