From a3502c419e621c6abac2a9e047e42a582fd80769 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 16 Mar 2006 06:17:54 +0000 Subject: Use association's :conditions when eager loading. [jeremyevans0@gmail.com] closes #4144 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3897 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 13 +++++++------ .../lib/active_record/associations/association_proxy.rb | 13 +++++++++++-- .../active_record/associations/belongs_to_association.rb | 2 +- .../associations/belongs_to_polymorphic_association.rb | 2 +- .../associations/has_and_belongs_to_many_association.rb | 8 ++++++-- .../lib/active_record/associations/has_many_association.rb | 5 ++--- .../associations/has_many_through_association.rb | 4 ++-- .../lib/active_record/associations/has_one_association.rb | 2 +- 8 files changed, 31 insertions(+), 18 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 072e35aadd..34fba0414c 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1157,7 +1157,7 @@ module ActiveRecord class JoinBase attr_reader :active_record - delegate :table_name, :column_names, :primary_key, :reflections, :to=>:active_record + delegate :table_name, :column_names, :primary_key, :reflections, :to => :active_record def initialize(active_record) @active_record = active_record @@ -1227,19 +1227,19 @@ module ActiveRecord def association_join join = case reflection.macro when :has_and_belongs_to_many - join_table_name = " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [ options[:join_table], aliased_join_table_name, aliased_join_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key, reflection.active_record.table_name, reflection.active_record.primary_key] + " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [ table_name, aliased_table_name, aliased_table_name, klass.primary_key, - options[:join_table], options[:association_foreign_key] || klass.table_name.classify.foreign_key + aliased_join_table_name, options[:association_foreign_key] || klass.table_name.classify.foreign_key ] when :has_many, :has_one case when reflection.macro == :has_many && reflection.options[:through] - through_reflection = parent.active_record.reflect_on_association(reflection.options[:through]) + through_reflection = parent.active_record.reflect_on_association(reflection.options[:through]) + through_conditions = through_reflection.options[:conditions] ? "AND #{eval("%(#{through_reflection.options[:conditions]})")}" : '' if through_reflection.options[:as] # has_many :through against a polymorphic join polymorphic_foreign_key = through_reflection.options[:as].to_s + '_id' polymorphic_foreign_type = through_reflection.options[:as].to_s + '_type' @@ -1284,11 +1284,12 @@ module ActiveRecord ] else "" - end + end || '' join << %(AND %s.%s = %s ) % [ aliased_table_name, reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), - klass.quote(klass.name)] unless join.blank? || klass.descends_from_active_record? + klass.quote(klass.name)] unless klass.descends_from_active_record? + join << "AND #{eval("%(#{reflection.options[:conditions]})")} " if reflection.options[:conditions] join end end diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index d340cc30e3..4ddeb84a58 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -14,14 +14,23 @@ module ActiveRecord def respond_to?(symbol, include_priv = false) proxy_respond_to?(symbol, include_priv) || (load_target && @target.respond_to?(symbol, include_priv)) end - + # Explicitly proxy === because the instance method removal above # doesn't catch it. def ===(other) load_target other === @target end - + + def aliased_table_name + @reflection.klass.table_name + end + + def conditions + @conditions ||= eval("%(#{@reflection.options[:conditions]})") if @reflection.options[:conditions] + end + alias :sql_conditions :conditions + def reset @target = nil @loaded = false diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 8955c0dc31..1752678cbd 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -43,7 +43,7 @@ module ActiveRecord def find_target @reflection.klass.find( @owner[@reflection.primary_key_name], - :conditions => @reflection.options[:conditions] ? interpolate_sql(@reflection.options[:conditions]) : nil, + :conditions => conditions, :include => @reflection.options[:include] ) end diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index 6d0704db74..9549b959fc 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -30,7 +30,7 @@ module ActiveRecord if @reflection.options[:conditions] association_class.find( @owner[@reflection.primary_key_name], - :conditions => interpolate_sql(@reflection.options[:conditions]), + :conditions => conditions, :include => @reflection.options[:include] ) else diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 5b203dcc57..25c23c63b7 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -144,7 +144,11 @@ module ActiveRecord @owner.connection.execute(sql) end end - + + #def aliased_join_table_name + # @reflection.options[:join_table] + #end + def construct_sql interpolate_sql_options!(@reflection.options, :finder_sql) @@ -152,7 +156,7 @@ module ActiveRecord @finder_sql = @reflection.options[:finder_sql] else @finder_sql = "#{@reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{@owner.quoted_id} " - @finder_sql << " AND (#{interpolate_sql(@reflection.options[:conditions])})" if @reflection.options[:conditions] + @finder_sql << " AND (#{conditions})" if conditions end @join_sql = "JOIN #{@reflection.options[:join_table]} ON #{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{@reflection.options[:join_table]}.#{@reflection.association_foreign_key}" diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 8b471859c1..8d82de0d01 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -3,7 +3,6 @@ module ActiveRecord class HasManyAssociation < AssociationCollection #:nodoc: def initialize(owner, reflection) super - @conditions = sanitize_sql(reflection.options[:conditions]) construct_sql end @@ -169,11 +168,11 @@ module ActiveRecord @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}" - @finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions + @finder_sql << " AND (#{conditions})" if conditions else @finder_sql = "#{@reflection.klass.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" - @finder_sql << " AND (#{interpolate_sql(@conditions)})" if @conditions + @finder_sql << " AND (#{conditions})" if conditions end if @reflection.options[:counter_sql] 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 6db5eae0bc..009ac7c53a 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -74,7 +74,7 @@ module ActiveRecord "AND #{through_reflection.table_name}.#{through_reflection.primary_key_name} = #{@owner.quoted_id}" end - conditions << " AND (#{interpolate_sql(sanitize_sql(@reflection.options[:conditions]))})" if @reflection.options[:conditions] + conditions << " AND (#{sql_conditions})" if sql_conditions return conditions end @@ -100,7 +100,7 @@ module ActiveRecord @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 + @finder_sql << " AND (#{conditions})" if conditions end if @reflection.options[:counter_sql] diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index e0d6f64b27..e881d49420 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -73,7 +73,7 @@ module ActiveRecord else @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" end - @finder_sql << " AND (#{sanitize_sql(@reflection.options[:conditions])})" if @reflection.options[:conditions] + @finder_sql << " AND (#{conditions})" if conditions end end end -- cgit v1.2.3