diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index f3eaffee84ad8e5db3730488794c284418301d7d..48b14f52cb3cd1bd511433f96aa8aa3e7a8b7f1d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* made method missing delegation to class methods on relation target work on :through associations. [Tobias Luetke] + +* made .find() work on :through relations. [Tobias Luetke] + * added :piggyback option to has_many :through relationships to pick up values from the join table as needed [Tobias Luetke] * Fix typo in association docs. #3296. [Blair Zajac] 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 3a3966bb8a7b573ce0a22f788572876ec39c562b..ea338e4658bd7880418d638a5d60e2415fe14e7d 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 @@ def find(*args) 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 @@ def construct_select 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 b8925a6ee5efdd79db391d71d1b85d3f5df7bd18..c533b3a873dcb50a60b4575c61579546225c3d6c 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -853,7 +853,7 @@ def with_scope(method_scoping = {}) 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 @@ def type_name_with_module(type_name) 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]) diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index 502e4d28c51a5e9c486a7a93ced2aaf34680bb9b..f385bd2175be380dcc6a932226b540a733f568aa 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -42,5 +42,23 @@ def test_polymorphic_has_many_going_through_join_model_with_inheritance def test_has_many_with_piggyback assert_equal "2", categories(:sti_test).authors.first.post_id end + + def test_has_many_find_all + assert_equal [categories(:general)], authors(:david).categories.find(:all) + end + + def test_has_many_find_first + assert_equal categories(:general), authors(:david).categories.find(:first) + end + + def test_has_many_find_conditions + assert_equal categories(:general), authors(:david).categories.find(:first, :conditions => "categories.name = 'General'") + assert_equal nil, authors(:david).categories.find(:first, :conditions => "categories.name = 'Technology'") + end + + def test_has_many_class_methods_called_by_method_missing + assert_equal categories(:general), authors(:david).categories.find_by_name('General') +# assert_equal nil, authors(:david).categories.find_by_name('Technology') + end end