diff options
Diffstat (limited to 'activerecord/lib')
5 files changed, 33 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 45d23eac1d..ff6775e027 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -323,9 +323,11 @@ module ActiveRecord # * <tt>:counter_sql</tt> - specify a complete SQL statement to fetch the size of the association. If +:finder_sql+ is # specified but +:counter_sql+, +:counter_sql+ will be generated by replacing SELECT ... FROM with SELECT COUNT(*) FROM. # * <tt>:extend</tt> - specify a named module for extending the proxy, see "Association extensions". + # * <tt>:include</tt> - specify second-order associations that should be eager loaded when the collection is loaded. # # Option examples: # has_many :comments, :order => "posted_on" + # has_many :comments, :include => :author # has_many :people, :class_name => "Person", :conditions => "deleted = 0", :order => "name" # has_many :tracks, :order => "position", :dependent => true # has_many :subscribers, :class_name => "Person", :finder_sql => @@ -336,7 +338,7 @@ module ActiveRecord def has_many(association_id, options = {}, &extension) options.assert_valid_keys( :foreign_key, :class_name, :exclusively_dependent, :dependent, - :conditions, :order, :finder_sql, :counter_sql, + :conditions, :order, :include, :finder_sql, :counter_sql, :before_add, :after_add, :before_remove, :after_remove, :extend ) @@ -417,13 +419,14 @@ module ActiveRecord # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name # of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_one association will use "person_id" # as the default foreign_key. + # * <tt>:include</tt> - specify second-order associations that should be eager loaded when this object is loaded. # # Option examples: # has_one :credit_card, :dependent => true # has_one :last_comment, :class_name => "Comment", :order => "posted_on" # has_one :project_manager, :class_name => "Person", :conditions => "role = 'project_manager'" def has_one(association_id, options = {}) - options.assert_valid_keys(:class_name, :foreign_key, :remote, :conditions, :order, :dependent, :counter_cache, :extend) + options.assert_valid_keys(:class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend) association_name, association_class_name, association_class_primary_key_name = associate_identification(association_id, options[:class_name], options[:foreign_key], false) @@ -496,6 +499,7 @@ module ActiveRecord # and decrement_counter. The counter cache is incremented when an object of this class is created and decremented when it's # destroyed. This requires that a column named "#{table_name}_count" (such as comments_count for a belonging Comment class) # is used on the associate class (such as a Post class). + # * <tt>:include</tt> - specify second-order associations that should be eager loaded when this object is loaded. # # Option examples: # belongs_to :firm, :foreign_key => "client_of" @@ -503,7 +507,7 @@ module ActiveRecord # belongs_to :valid_coupon, :class_name => "Coupon", :foreign_key => "coupon_id", # :conditions => 'discounts > #{payments_count}' def belongs_to(association_id, options = {}) - options.assert_valid_keys(:class_name, :foreign_key, :remote, :conditions, :order, :dependent, :counter_cache, :extend) + options.assert_valid_keys(:class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend) association_name, association_class_name, class_primary_key_name = associate_identification(association_id, options[:class_name], options[:foreign_key], false) @@ -613,16 +617,18 @@ module ActiveRecord # * <tt>:insert_sql</tt> - overwrite the default generated SQL used to add links between the associated classes # with a manual one # * <tt>:extend</tt> - anonymous module for extending the proxy, see "Association extensions". + # * <tt>:include</tt> - specify second-order associations that should be eager loaded when the collection is loaded. # # Option examples: # has_and_belongs_to_many :projects + # has_and_belongs_to_many :projects, :include => [ :milestones, :manager ] # has_and_belongs_to_many :nations, :class_name => "Country" # has_and_belongs_to_many :categories, :join_table => "prods_cats" # has_and_belongs_to_many :active_projects, :join_table => 'developers_projects', :delete_sql => # 'DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}' def has_and_belongs_to_many(association_id, options = {}, &extension) options.assert_valid_keys( - :class_name, :table_name, :foreign_key, :association_foreign_key, :conditions, + :class_name, :table_name, :foreign_key, :association_foreign_key, :conditions, :include, :join_table, :finder_sql, :delete_sql, :insert_sql, :order, :uniq, :before_add, :after_add, :before_remove, :after_remove, :extend ) diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 9dc0e26e10..528c42cea7 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -49,9 +49,13 @@ module ActiveRecord private def find_target if @options[:conditions] - @association_class.find_on_conditions(@owner[@association_class_primary_key_name], interpolate_sql(@options[:conditions])) + @association_class.find( + @owner[@association_class_primary_key_name], + :conditions => interpolate_sql(@options[:conditions]), + :include => @options[:include] + ) else - @association_class.find(@owner[@association_class_primary_key_name]) + @association_class.find(@owner[@association_class_primary_key_name], :include => @options[:include]) end end 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 a90d89a69b..2a51efb94f 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 @@ -90,7 +90,7 @@ module ActiveRecord if @options[:finder_sql] records = @association_class.find_by_sql(@finder_sql) else - records = find(:all) + records = find(:all, :include => @options[:include]) end @options[:uniq] ? uniq(records) : records diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 9ee06ffbcb..3027e26ce0 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -23,12 +23,12 @@ module ActiveRecord # DEPRECATED. def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) if @options[:finder_sql] - records = @association_class.find_by_sql(@finder_sql) + @association_class.find_by_sql(@finder_sql) else - sql = @finder_sql - sql += " AND (#{sanitize_sql(runtime_conditions)})" if runtime_conditions + conditions = @finder_sql + conditions += " AND (#{sanitize_sql(runtime_conditions)})" if runtime_conditions orderings ||= @options[:order] - records = @association_class.find_all(sql, orderings, limit, joins) + @association_class.find_all(conditions, orderings, limit, joins) end end @@ -105,7 +105,17 @@ module ActiveRecord end def find_target - find_all + if @options[:finder_sql] + @association_class.find_by_sql(@finder_sql) + else + @association_class.find(:all, + :conditions => @finder_sql, + :order => @options[:order], + :limit => @options[:limit], + :joins => @options[:joins], + :include => @options[:include] + ) + end end def count_records diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 18f84ee98f..edd9e53c19 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -57,7 +57,7 @@ module ActiveRecord private def find_target - @association_class.find(:first, :conditions => @finder_sql, :order => @options[:order]) + @association_class.find(:first, :conditions => @finder_sql, :order => @options[:order], :include => @options[:include]) end def target_obsolete? |