Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added :include as an option for association declarations [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2898 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 49c801b71d425543ae88e171b969b3316f022c2a 1 parent 89ec75e
@dhh dhh authored
View
4 activerecord/CHANGELOG
@@ -1,5 +1,9 @@
*SVN*
+* Added :include as an option for association declarations [DHH]. Example:
+
+ has_many :posts, :include => [ :author, :comments ]
+
* Rename Base.constrain to Base.with_scope so it doesn't conflict with existing concept of database constraints. Make scoping more robust: uniform method => parameters, validated method names and supported finder parameters, raise exception on nested scopes. [Jeremy Kemper] Example:
Comment.with_scope(:find => { :conditions => 'active=true' }, :create => { :post_id => 5 }) do
View
14 activerecord/lib/active_record/associations.rb
@@ -323,9 +323,11 @@ module ClassMethods
# * <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 ClassMethods
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 @@ def has_many(association_id, options = {}, &extension)
# * <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 @@ def has_one(association_id, options = {})
# 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 @@ def has_one(association_id, options = {})
# 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 @@ def belongs_to(association_id, options = {})
# * <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
)
View
8 activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -49,9 +49,13 @@ def updated?
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
View
2  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -90,7 +90,7 @@ def find_target
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
View
20 activerecord/lib/active_record/associations/has_many_association.rb
@@ -23,12 +23,12 @@ def build(attributes = {})
# 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 @@ def method_missing(method, *args, &block)
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
View
2  activerecord/lib/active_record/associations/has_one_association.rb
@@ -57,7 +57,7 @@ def replace(obj, dont_save = false)
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?
View
25 activerecord/test/associations_go_eager_test.rb
@@ -186,4 +186,29 @@ def test_eager_with_valid_association_as_string_not_symbol
assert_nothing_raised { Post.find(:all, :include => 'comments') }
end
+ def test_preconfigured_includes_with_belongs_to
+ author = posts(:welcome).author_with_posts
+ assert_equal 5, author.posts.size
+ end
+
+ def test_preconfigured_includes_with_has_one
+ comment = posts(:sti_comments).very_special_comment_with_post
+ assert_equal posts(:sti_comments), comment.post
+ end
+
+ def test_preconfigured_includes_with_has_many
+ posts = authors(:david).posts_with_comments
+ assert_equal 2, posts.first.comments.size
+ end
+
+ def test_preconfigured_includes_with_habtm
+ posts = authors(:david).posts_with_categories
+ assert_equal 2, posts.first.categories.size
+ end
+
+ def test_preconfigured_includes_with_has_many_and_habtm
+ posts = authors(:david).posts_with_comments_and_categories
+ assert_equal 2, posts.first.comments.size
+ assert_equal 2, posts.first.categories.size
+ end
end
View
4 activerecord/test/fixtures/author.rb
@@ -1,5 +1,9 @@
class Author < ActiveRecord::Base
has_many :posts
+ has_many :posts_with_comments, :include => :comments, :class_name => "Post"
+ has_many :posts_with_categories, :include => :categories, :class_name => "Post"
+ has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :class_name => "Post"
+
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
:after_add => :log_after_adding, :before_remove => :log_before_removing,
:after_remove => :log_after_removing
View
3  activerecord/test/fixtures/post.rb
@@ -5,6 +5,8 @@ def greeting
end
end
+ belongs_to :author_with_posts, :class_name => "Author", :include => :posts
+
has_many :comments, :order => "body" do
def find_most_recent
find(:first, :order => "id DESC")
@@ -12,6 +14,7 @@ def find_most_recent
end
has_one :very_special_comment
+ has_one :very_special_comment_with_post, :class_name => "VerySpecialComment", :include => :post
has_many :special_comments
has_and_belongs_to_many :categories
Please sign in to comment.
Something went wrong with that request. Please try again.