Added association preload to relation.
This commit is contained in:
parent
6b67df70ab
commit
c01c21b31d
@ -664,11 +664,28 @@ def last(*args)
|
||||
# This is an alias for find(:all). You can pass in all the same arguments to this method as you can
|
||||
# to find(:all)
|
||||
def all(*args)
|
||||
if args.empty? && !scoped?(:find)
|
||||
arel_table
|
||||
options = args.extract_options!
|
||||
|
||||
|
||||
if options.empty? #&& !scoped?(:find)
|
||||
relation = arel_table
|
||||
else
|
||||
construct_finder_arel(*args)
|
||||
include_associations = merge_includes(scope(:find, :include), options[:include])
|
||||
|
||||
# if include_associations.any? && references_eager_loaded_tables?(options)
|
||||
# join_dependency = JoinDependency.new(self, include_associations, options[:joins])
|
||||
|
||||
# relation = construct_finder_arel_with_included_associations(options, join_dependency)
|
||||
|
||||
# relation.preload(include_associations)
|
||||
# else
|
||||
relation = construct_finder_arel(options)
|
||||
if include_associations.any?
|
||||
relation.preload(include_associations)
|
||||
# end
|
||||
end
|
||||
end
|
||||
relation
|
||||
end
|
||||
|
||||
# Executes a custom SQL query against your database and returns all the results. The results will
|
||||
|
@ -6,6 +6,12 @@ class Relation
|
||||
def initialize(klass, relation)
|
||||
@klass, @relation = klass, relation
|
||||
@readonly = false
|
||||
@associations_to_preload = []
|
||||
end
|
||||
|
||||
def preload(association)
|
||||
@associations_to_preload << association
|
||||
@associations_to_preload.flatten!
|
||||
end
|
||||
|
||||
def readonly
|
||||
@ -16,6 +22,8 @@ def readonly
|
||||
def to_a
|
||||
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
|
||||
|
||||
records
|
||||
|
@ -1,6 +1,7 @@
|
||||
require "cases/helper"
|
||||
require 'models/post'
|
||||
require 'models/topic'
|
||||
require 'models/comment'
|
||||
require 'models/reply'
|
||||
require 'models/author'
|
||||
require 'models/entrant'
|
||||
@ -8,7 +9,7 @@
|
||||
require 'models/company'
|
||||
|
||||
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
|
||||
assert_equal Author.find(:all, :conditions => "name = 'David'"), Author.all.conditions("name = 'David'").to_a
|
||||
@ -85,5 +86,33 @@ def test_find_with_readonly_option
|
||||
Developer.all.readonly.each { |d| assert d.readonly? }
|
||||
Developer.all(:readonly => true).each { |d| assert d.readonly? }
|
||||
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
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user