diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-03 12:06:23 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-03 12:06:23 +0000 |
commit | efb55d1cf50ba895b2811424b8df5e65f1205a66 (patch) | |
tree | 4eefcc45a9abd0096c9166e6487febf8dfecbd97 /activerecord/lib/active_record/associations.rb | |
parent | 4f6c943b05ea537be515ae6d70f8986191ef7e30 (diff) | |
download | rails-efb55d1cf50ba895b2811424b8df5e65f1205a66.tar.gz rails-efb55d1cf50ba895b2811424b8df5e65f1205a66.tar.bz2 rails-efb55d1cf50ba895b2811424b8df5e65f1205a66.zip |
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
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index ba6a0ab89b..956589fa84 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -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 |