Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use ARel in SQL generation through associations

  • Loading branch information...
commit 8c3b8323f57d366fc308e773b286a1847552b0a3 1 parent 103b282
@miloops miloops authored
View
26 activerecord/lib/active_record/associations.rb
@@ -1590,19 +1590,23 @@ def select_all_rows(options, join_dependency)
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 @@ def add_limited_ids_condition!(sql, options, join_dependency)
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
View
8 activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -32,7 +32,7 @@ def size
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 @@ def construct_find_options!(options)
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 @@ def construct_conditions
end
def construct_from
- @reflection.quoted_table_name
+ @reflection.table_name
end
def construct_select(custom_select = nil)
@@ -239,7 +239,7 @@ def build_through_conditions
interpolate_sql(sanitize_sql(conditions))
end
end
-
+
def build_sti_condition
@reflection.through_reflection.klass.send(:type_condition)
end
Please sign in to comment.
Something went wrong with that request. Please try again.