aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPivotal Labs <contact@pivotallabs.com>2008-09-23 13:32:17 -0700
committerMichael Koziarski <michael@koziarski.com>2008-09-24 13:26:06 +0200
commit487758b3b88a38da3a75900839aea03774904fe1 (patch)
treee24091c573d18f80dde8b8dc8c214d4741515b37 /activerecord/lib
parent70b8ea4fa6f432919340345ae0d5af6aa8f87ec8 (diff)
downloadrails-487758b3b88a38da3a75900839aea03774904fe1.tar.gz
rails-487758b3b88a38da3a75900839aea03774904fe1.tar.bz2
rails-487758b3b88a38da3a75900839aea03774904fe1.zip
Allowed passing arrays-of-strings to :join everywhere. Merge duplicate join strings to avoid table aliasing problems.
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#1077 state:committed]
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/base.rb34
1 files changed, 21 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 3aa8e5541d..69ea155ace 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1576,19 +1576,19 @@ module ActiveRecord #:nodoc:
(safe_to_array(first) + safe_to_array(second)).uniq
end
- def merge_joins(first, second)
- if first.is_a?(String) && second.is_a?(String)
- "#{first} #{second}"
- elsif first.is_a?(String) || second.is_a?(String)
- if first.is_a?(String)
- join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, second, nil)
- "#{first} #{join_dependency.join_associations.collect { |assoc| assoc.association_join }.join}"
- else
- join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, first, nil)
- "#{join_dependency.join_associations.collect { |assoc| assoc.association_join }.join} #{second}"
+ def merge_joins(*joins)
+ if joins.any?{|j| j.is_a?(String) || array_of_strings?(j) }
+ joins = joins.collect do |join|
+ join = [join] if join.is_a?(String)
+ unless array_of_strings?(join)
+ join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, join, nil)
+ join = join_dependency.join_associations.collect { |assoc| assoc.association_join }
+ end
+ join
end
+ joins.flatten.uniq
else
- (safe_to_array(first) + safe_to_array(second)).uniq
+ joins.collect{|j| safe_to_array(j)}.flatten.uniq
end
end
@@ -1604,6 +1604,10 @@ module ActiveRecord #:nodoc:
end
end
+ def array_of_strings?(o)
+ o.is_a?(Array) && o.all?{|obj| obj.is_a?(String)}
+ end
+
def add_order!(sql, order, scope = :auto)
scope = scope(:find) if :auto == scope
scoped_order = scope[:order] if scope
@@ -1652,8 +1656,12 @@ module ActiveRecord #:nodoc:
merged_joins = scope && scope[:joins] && joins ? merge_joins(scope[:joins], joins) : (joins || scope && scope[:joins])
case merged_joins
when Symbol, Hash, Array
- join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, merged_joins, nil)
- sql << " #{join_dependency.join_associations.collect { |assoc| assoc.association_join }.join} "
+ if array_of_strings?(merged_joins)
+ sql << merged_joins.join(' ') + " "
+ else
+ join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, merged_joins, nil)
+ sql << " #{join_dependency.join_associations.collect { |assoc| assoc.association_join }.join} "
+ end
when String
sql << " #{merged_joins} "
end