diff options
author | Tobias Lütke <tobias.luetke@gmail.com> | 2005-12-20 21:20:35 +0000 |
---|---|---|
committer | Tobias Lütke <tobias.luetke@gmail.com> | 2005-12-20 21:20:35 +0000 |
commit | 90099e9dc287d46178581d6b09d8b42a0a302a04 (patch) | |
tree | d58f41dcc9eef3c54c0ff76bf8f131b6898c2d1d /activerecord/lib/active_record/associations | |
parent | d5441b2d506b4286e92746abd8c919e4ce10380e (diff) | |
download | rails-90099e9dc287d46178581d6b09d8b42a0a302a04.tar.gz rails-90099e9dc287d46178581d6b09d8b42a0a302a04.tar.bz2 rails-90099e9dc287d46178581d6b09d8b42a0a302a04.zip |
made .find() and class method delegation work on :through relations
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3325 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 35 |
1 files changed, 33 insertions, 2 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 |