aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-06-23 17:38:31 -0300
committerEmilio Tagua <miloops@gmail.com>2009-06-23 17:38:31 -0300
commit02a723f7b5f30b00fc77bc85162598f707c7b682 (patch)
treef3761e591165cac781455b7fbf27b1ba316a3e90 /activerecord/lib/active_record/associations.rb
parentbc899fd3a37fae7e0024e8f0439f204818aeafd7 (diff)
downloadrails-02a723f7b5f30b00fc77bc85162598f707c7b682.tar.gz
rails-02a723f7b5f30b00fc77bc85162598f707c7b682.tar.bz2
rails-02a723f7b5f30b00fc77bc85162598f707c7b682.zip
Arel now buils SQL queries for associations. Removed old code and
updated Arel version to support this.
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb53
1 files changed, 6 insertions, 47 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 29f58a04e6..97fdd9b1ba 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1604,14 +1604,6 @@ module ActiveRecord
return sanitize_sql(arel.to_sql)
end
- def add_limited_ids_condition!(sql, options, join_dependency)
- unless (id_list = select_limited_ids_list(options, join_dependency)).empty?
- sql << "#{condition_word(sql)} #{connection.quote_table_name table_name}.#{primary_key} IN (#{id_list}) "
- else
- throw :invalid_query
- end
- end
-
def construct_limited_ids_condition(where, options, join_dependency)
unless (id_list = select_limited_ids_list(options, join_dependency)).empty?
"#{where.blank? ? '' : ' AND '} #{connection.quote_table_name table_name}.#{primary_key} IN (#{id_list}) "
@@ -1630,47 +1622,18 @@ module ActiveRecord
end
def construct_finder_sql_for_association_limiting(options, join_dependency)
- scope = scope(:find)
-
# Only join tables referenced in order or conditions since this is particularly slow on the pre-query.
tables_from_conditions = conditions_tables(options)
tables_from_order = order_tables(options)
all_tables = tables_from_conditions + tables_from_order
- distinct_join_associations = all_tables.uniq.map{|table|
+ options[:joins] = all_tables.uniq.map {|table|
join_dependency.joins_for_table_name(table)
- }.flatten.compact.uniq
-
- order = options[:order]
- if scoped_order = (scope && scope[:order])
- order = order ? "#{order}, #{scoped_order}" : scoped_order
- end
-
- is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order)
- sql = "SELECT "
- if is_distinct
- sql << connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", order)
- else
- sql << primary_key
- end
- sql << " FROM #{connection.quote_table_name table_name} "
+ }.flatten.compact.uniq.collect { |assoc| assoc.association_join }.join
- if is_distinct
- sql << distinct_join_associations.collect { |assoc| assoc.association_join }.join
- add_joins!(sql, options[:joins], scope)
- end
-
- add_conditions!(sql, options[:conditions], scope)
- add_group!(sql, options[:group], options[:having], scope)
-
- if order && is_distinct
- connection.add_order_by_for_association_limiting!(sql, :order => order)
- else
- add_order!(sql, options[:order], scope)
- end
-
- add_limit!(sql, options, scope)
-
- return sanitize_sql(sql)
+ construct_finder_sql(options.merge(
+ :select => connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", construct_order(options[:order], scope(:find)).join(","))
+ )
+ )
end
def tables_in_string(string)
@@ -1774,10 +1737,6 @@ module ActiveRecord
end
end
- def condition_word(sql)
- sql =~ /where/i ? " AND " : "WHERE "
- end
-
def create_extension_modules(association_id, block_extension, extensions)
if block_extension
extension_module_name = "#{self.to_s.demodulize}#{association_id.to_s.camelize}AssociationExtension"