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
This commit is contained in:
parent
89ec75edc9
commit
49c801b71d
|
@ -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
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -186,4 +186,29 @@ class EagerAssociationTest < Test::Unit::TestCase
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -5,6 +5,8 @@ class Post < ActiveRecord::Base
|
|||
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 @@ class Post < ActiveRecord::Base
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue