aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-06-10 16:36:05 -0300
committerEmilio Tagua <miloops@gmail.com>2009-06-10 16:36:05 -0300
commit8c3b8323f57d366fc308e773b286a1847552b0a3 (patch)
treea558bbfe9b50c5fcfacfe62c7a45db60f17a1ab9 /activerecord/lib
parent103b282130dd340143654801430aed787da4c9c6 (diff)
downloadrails-8c3b8323f57d366fc308e773b286a1847552b0a3.tar.gz
rails-8c3b8323f57d366fc308e773b286a1847552b0a3.tar.bz2
rails-8c3b8323f57d366fc308e773b286a1847552b0a3.zip
Use ARel in SQL generation through associations
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb26
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb8
2 files changed, 19 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index a74e9b76dc..4ed0cb70e9 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1590,19 +1590,23 @@ module ActiveRecord
def construct_finder_sql_with_included_associations(options, join_dependency)
scope = scope(:find)
- sql = "SELECT #{column_aliases(join_dependency)} FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
- sql << join_dependency.join_associations.collect{|join| join.association_join }.join
- add_joins!(sql, options[:joins], scope)
- add_conditions!(sql, options[:conditions], scope)
- add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
+ joins = join_dependency.join_associations.collect{|join| join.association_join }.join
+ joins << construct_join(options[:joins], scope)
- add_group!(sql, options[:group], options[:having], scope)
- add_order!(sql, options[:order], scope)
- add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections)
- add_lock!(sql, options, scope)
+ conditions = construct_conditions(options[:conditions], scope) || ''
+ conditions << construct_limited_ids_condition(conditions, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
- return sanitize_sql(sql)
+ arel = arel_table((scope && scope[:from]) || options[:from] || table_name).
+ join(joins).
+ where(conditions).
+ project(column_aliases(join_dependency)).
+ group(construct_group(options[:group], options[:having], scope)).
+ order(construct_order(options[:order], scope))
+
+ arel = arel.take(construct_limit(options, scope)) if using_limitable_reflections?(join_dependency.reflections)
+
+ return sanitize_sql(arel.to_sql)
end
def add_limited_ids_condition!(sql, options, join_dependency)
@@ -1615,7 +1619,7 @@ module ActiveRecord
def construct_limited_ids_condition(where, options, join_dependency)
unless (id_list = select_limited_ids_list(options, join_dependency)).empty?
- "#{where.blank? ? 'WHERE ' : ' AND '} #{connection.quote_table_name table_name}.#{primary_key} IN (#{id_list}) "
+ "#{where.blank? ? '' : ' AND '} #{connection.quote_table_name table_name}.#{primary_key} IN (#{id_list}) "
else
throw :invalid_query
end
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index e8dbae9011..51fb75e1f5 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -32,7 +32,7 @@ module ActiveRecord
return @target.size if loaded?
return count
end
-
+
protected
def target_reflection_has_associated_record?
if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.primary_key_name].blank?
@@ -48,7 +48,7 @@ module ActiveRecord
options[:joins] = construct_joins(options[:joins])
options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil?
end
-
+
def insert_record(record, force = true, validate = true)
if record.new_record?
if force
@@ -131,7 +131,7 @@ module ActiveRecord
end
def construct_from
- @reflection.quoted_table_name
+ @reflection.table_name
end
def construct_select(custom_select = nil)
@@ -239,7 +239,7 @@ module ActiveRecord
interpolate_sql(sanitize_sql(conditions))
end
end
-
+
def build_sti_condition
@reflection.through_reflection.klass.send(:type_condition)
end