mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Allow order, conditions, and joins in finds that include associations
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1080 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
4f6c943b05
commit
efb55d1cf5
3 changed files with 17 additions and 5 deletions
|
@ -621,7 +621,7 @@ module ActiveRecord
|
|||
|
||||
def find_with_associations(options = {})
|
||||
reflections = [ options[:include] ].flatten.collect { |association| reflect_on_association(association) }
|
||||
rows = connection.select_all(construct_finder_sql_with_included_associations(reflections), "#{name} Load Including Associations")
|
||||
rows = connection.select_all(construct_finder_sql_with_included_associations(options, reflections), "#{name} Load Including Associations")
|
||||
records = rows.collect { |row| instantiate(extract_record(table_name, row)) }.uniq
|
||||
|
||||
reflections.each do |reflection|
|
||||
|
@ -638,14 +638,19 @@ module ActiveRecord
|
|||
return records
|
||||
end
|
||||
|
||||
def construct_finder_sql_with_included_associations(reflections)
|
||||
def construct_finder_sql_with_included_associations(options, reflections)
|
||||
sql = "SELECT #{selected_columns(table_name, columns)}"
|
||||
reflections.each { |reflection| sql << ", #{selected_columns(reflection.klass.table_name, reflection.klass.columns)}" }
|
||||
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}"
|
||||
"#{reflection.klass.table_name}.#{table_name.classify.foreign_key} = #{table_name}.#{primary_key} "
|
||||
end
|
||||
|
||||
sql << "#{options[:joins]} " if options[:joins]
|
||||
add_conditions!(sql, options[:conditions])
|
||||
sql << "ORDER BY #{options[:order]} " if options[:order]
|
||||
|
||||
return sanitize_sql(sql)
|
||||
end
|
||||
|
|
|
@ -706,14 +706,17 @@ module ActiveRecord #:nodoc:
|
|||
sql << "#{options[:joins]} " if options[:joins]
|
||||
add_conditions!(sql, options[:conditions])
|
||||
sql << "ORDER BY #{options[:order]} " if options[:order]
|
||||
add_limit!(sql, options)
|
||||
|
||||
return sql
|
||||
end
|
||||
|
||||
def add_limit!(sql, options)
|
||||
if options[:limit] && options[:offset]
|
||||
connection.add_limit_with_offset!(sql, options[:limit].to_i, options[:offset].to_i)
|
||||
elsif options[:limit]
|
||||
connection.add_limit_without_offset!(sql, options[:limit].to_i)
|
||||
end
|
||||
|
||||
return sql
|
||||
end
|
||||
|
||||
# Adds a sanitized version of +conditions+ to the +sql+ string. Note that it's the passed +sql+ string is changed.
|
||||
|
|
|
@ -539,6 +539,10 @@ class HasManyAssociationsTest < Test::Unit::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue