From a3f3fab7955a0874bd58d1d997e344bdde710628 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 27 Dec 2009 17:37:36 +0530 Subject: Add relation.from as a temporary workaround until arel relation has .from option --- activerecord/lib/active_record/base.rb | 3 ++- activerecord/lib/active_record/relation.rb | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 302c9fb724..c0b8e7e14f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1632,7 +1632,8 @@ module ActiveRecord #:nodoc: group(construct_group(options[:group], options[:having], scope)). order(construct_order(options[:order], scope)). limit(construct_limit(options[:limit], scope)). - offset(construct_offset(options[:offset], scope)) + offset(construct_offset(options[:offset], scope)). + from(options[:from]) relation = relation.readonly if options[:readonly] diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index f1d1fa0790..87f6aa3643 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -28,6 +28,14 @@ module ActiveRecord create_new_relation(@relation.project(selects)) end + # TODO : This is temporary. We need .from in Arel. + attr_writer :from + def from(from) + relation = create_new_relation + relation.from = from + relation + end + def group(groups) create_new_relation(@relation.group(groups)) end @@ -94,7 +102,7 @@ module ActiveRecord :conditions => where_clause, :limit => @relation.taken, :offset => @relation.skipped, - :from => @relation.send(:table_sql, Arel::Sql::TableReference.new(@relation)) + :from => @from }, ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil)) end @@ -242,8 +250,10 @@ module ActiveRecord end end - def create_new_relation(relation, readonly = @readonly, preload = @associations_to_preload, eager_load = @eager_load_associations) - Relation.new(@klass, relation, readonly, preload, eager_load) + def create_new_relation(relation = @relation, readonly = @readonly, preload = @associations_to_preload, eager_load = @eager_load_associations) + r = self.class.new(@klass, relation, readonly, preload, eager_load) + r.from = @from + r end def where_clause(join_string = "\n\tAND ") -- cgit v1.2.3