diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 35 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 4 |
2 files changed, 35 insertions, 4 deletions
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 3a3966bb8a..ea338e4658 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -1,6 +1,14 @@ module ActiveRecord module Associations class HasManyThroughAssociation < AssociationProxy #:nodoc: + + def initialize(owner, reflection) + super + @finder_sql = construct_conditions + construct_sql + end + + def find(*args) options = Base.send(:extract_options_from_args!, args) @@ -15,7 +23,10 @@ module ActiveRecord elsif @reflection.options[:order] options[:order] = @reflection.options[:order] end - + + options[:select] = construct_select + options[:from] = construct_from + merge_options_from_reflection!(options) # Pass through args exactly as we received them. @@ -83,10 +94,30 @@ module ActiveRecord def construct_scope { - :find => { :conditions => construct_conditions }, + :find => { :from => construct_from, :conditions => construct_conditions }, :create => { @reflection.primary_key_name => @owner.id } } end + + def construct_sql + case + when @reflection.options[:finder_sql] + @finder_sql = interpolate_sql(@reflection.options[:finder_sql]) + + @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" + @finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions + end + + if @reflection.options[:counter_sql] + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + elsif @reflection.options[:finder_sql] + @reflection.options[:counter_sql] = @reflection.options[:finder_sql].gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM") + @counter_sql = interpolate_sql(@reflection.options[:counter_sql]) + else + @counter_sql = @finder_sql + end + end + end end end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index b8925a6ee5..c533b3a873 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -853,7 +853,7 @@ module ActiveRecord #:nodoc: method_scoping.assert_valid_keys [:find, :create] if f = method_scoping[:find] - f.assert_valid_keys [:conditions, :joins, :offset, :limit, :readonly] + f.assert_valid_keys [:conditions, :joins, :from, :offset, :limit, :readonly] f[:readonly] = true if !f[:joins].blank? && !f.has_key?(:readonly) end @@ -917,7 +917,7 @@ module ActiveRecord #:nodoc: def construct_finder_sql(options) sql = "SELECT #{options[:select] || '*'} " - sql << "FROM #{options[:from] || table_name} " + sql << "FROM #{scope(:find, :from) || options[:from] || table_name} " add_joins!(sql, options) add_conditions!(sql, options[:conditions]) |