From 8e3bf70bcd496ae642fdae5ad7717ad8378de176 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Mon, 9 Oct 2006 02:46:57 +0000 Subject: Removes the ability for eager loaded conditions to be interpolated, since there is no model instance to use as a context for interpolation. #5553 [turnip@turnipspatch.com] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5264 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 32 ++++++++-------------- .../associations/association_proxy.rb | 2 +- 2 files changed, 12 insertions(+), 22 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 93f10c666c..459484f8c2 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -349,23 +349,17 @@ module ActiveRecord # But that shouldn't fool you to think that you can pull out huge amounts of data with no performance penalty just because you've reduced # the number of queries. The database still needs to send all the data to Active Record and it still needs to be processed. So it's no # catch-all for performance problems, but it's a great way to cut down on the number of queries in a situation as the one described above. + # + # Since the eager loading pulls from multiple tables, you'll have to disambiguate any column references in both conditions and orders. So + # :order => "posts.id DESC" will work while :order => "id DESC" will not. Because eager loading generates the SELECT statement too, the + # :select option is ignored. # - # Please note that limited eager loading with has_many and has_and_belongs_to_many associations is not compatible with describing conditions - # on these eager tables. This will work: - # - # Post.find(:all, :include => :comments, :conditions => "posts.title = 'magic forest'", :limit => 2) - # - # ...but this will not (and an ArgumentError will be raised): - # - # Post.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%'", :limit => 2) - # - # Also have in mind that since the eager loading is pulling from multiple tables, you'll have to disambiguate any column references - # in both conditions and orders. So :order => "posts.id DESC" will work while :order => "id DESC" will not. This may require that - # you alter the :order and :conditions on the association definitions themselves. Because eager loading generates the SELECT statement too, - # the :select option is ignored. - # - # It's currently not possible to use eager loading on multiple associations from the same table. Eager loading will not pull - # additional attributes on join tables, so "rich associations" with has_and_belongs_to_many is not a good fit for eager loading. + # You can use eager loading on multiple associations from the same table, but you cannot use those associations in orders and conditions + # as there is currently not any way to disambiguate them. Eager loading will not pull additional attributes on join tables, so "rich + # associations" with has_and_belongs_to_many are not a good fit for eager loading. + # + # When eager loaded, conditions are not interpolated. This is because the interpolation of lazy loaded conditions happens within the context + # of the object; when eager loading the object does not exist yet. # # == Table Aliasing # @@ -1567,7 +1561,7 @@ module ActiveRecord aliased_table_name, reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record? - join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions] + join << "AND #{sanitize_sql(reflection.options[:conditions])} " if reflection.options[:conditions] join end @@ -1583,10 +1577,6 @@ module ActiveRecord def table_name_and_alias table_alias_for table_name, @aliased_table_name end - - def interpolate_sql(sql) - instance_eval("%@#{sql.gsub('@', '\@')}@") - end end end end diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 4cdbc52ccf..1b929f28bd 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -41,7 +41,7 @@ module ActiveRecord end def conditions - @conditions ||= eval("%(#{@reflection.klass.send :sanitize_sql, @reflection.options[:conditions]})") if @reflection.options[:conditions] + @conditions ||= interpolate_sql(sanitize_sql(@reflection.options[:conditions])) if @reflection.options[:conditions] end alias :sql_conditions :conditions -- cgit v1.2.3