diff options
Diffstat (limited to 'lib/active_relation')
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 14 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 12 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 11 |
3 files changed, 23 insertions, 14 deletions
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index ddf5ef5e07..d78e940ffc 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -63,31 +63,31 @@ module ActiveRelation module Predications def eq(other) - Equality.new(self, other) + Equality.new(self, other.bind(relation)) end def lt(other) - LessThan.new(self, other) + LessThan.new(self, other.bind(relation)) end def lteq(other) - LessThanOrEqualTo.new(self, other) + LessThanOrEqualTo.new(self, other.bind(relation)) end def gt(other) - GreaterThan.new(self, other) + GreaterThan.new(self, other.bind(relation)) end def gteq(other) - GreaterThanOrEqualTo.new(self, other) + GreaterThanOrEqualTo.new(self, other.bind(relation)) end def matches(regexp) - Match.new(self, regexp) + Match.new(self, regexp.bind(relation)) end def in(array) - In.new(self, array) + In.new(self, array.bind(relation)) end end include Predications diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 1364911f0c..fa66bd039b 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -105,12 +105,12 @@ module ActiveRelation formatter.select [ "SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectExpression.new(engine)) }.join(', ')}", "FROM #{table_sql}", - (joins unless joins.blank? ), - ("WHERE #{selects.collect {|s| s.to_sql(Sql::WhereClause.new(engine))}.join("\n\tAND ")}" unless selects.blank? ), - ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank? ), - ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), - ("LIMIT #{limit}" unless limit.blank? ), - ("OFFSET #{offset}" unless offset.blank? ) + (joins unless joins.blank? ), + ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(engine)) }.join("\n\tAND ")}" unless selects.blank? ), + ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(engine)) }.join(', ')}" unless orders.blank? ), + ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), + ("LIMIT #{limit}" unless limit.blank? ), + ("OFFSET #{offset}" unless offset.blank? ) ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 027356d4d7..3a773fdf6e 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -24,12 +24,21 @@ module ActiveRelation end end - class WhereClause < Formatter + class PassThrough < Formatter def value(value) value end end + class WhereClause < PassThrough + end + + class OrderClause < PassThrough + def attribute(relation_name, attribute_name, aliaz) + "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" + end + end + class WhereCondition < Formatter def attribute(relation_name, attribute_name, aliaz) "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}" |