提交 c23c9bd1 编写于 作者: R Rick Olson

Allow association scoping for built/created records if :conditions is...

Allow association scoping for built/created records if :conditions is specified as a hash.  Closes #11393 [miloops]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9068 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 273b21fa
......@@ -109,7 +109,7 @@ def clear_association_cache #:nodoc:
#
# == Auto-generated methods
#
# ===Singular associations (one-to-one)
# === Singular associations (one-to-one)
# | | belongs_to |
# generated methods | belongs_to | :polymorphic | has_one
# ----------------------------------+------------+--------------+---------
......@@ -639,7 +639,9 @@ module ClassMethods
# from the association name. So <tt>has_many :products</tt> will by default be linked to the +Product+ class, but
# if the real class name is +SpecialProduct+, you'll have to specify it with this option.
# * <tt>:conditions</tt> - specify the conditions that the associated objects must meet in order to be included as a +WHERE+
# SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>.
# SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>. Record creations from the association are scoped if a hash
# is used. <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt>
# or <tt>@blog.posts.build</tt>.
# * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment,
# such as <tt>last_name, first_name DESC</tt>
# * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
......@@ -981,7 +983,9 @@ def belongs_to(association_id, options = {})
# guessed to be the name of the associated class in lower-case and +_id+ suffixed. So if the associated class is +Project+,
# the +has_and_belongs_to_many+ association will use +project_id+ as the default association +foreign_key+.
# * <tt>:conditions</tt> - specify the conditions that the associated object must meet in order to be included as a +WHERE+
# SQL fragment, such as <tt>authorized = 1</tt>.
# SQL fragment, such as <tt>authorized = 1</tt>. Record creations from the association are scoped if a hash is used.
# <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt>
# or <tt>@blog.posts.build</tt>.
# * <tt>:order</tt> - specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment,
# such as <tt>last_name, first_name DESC</tt>
# * <tt>:uniq</tt> - if set to +true+, duplicate associated objects will be ignored by accessors and query methods
......
......@@ -202,6 +202,7 @@ def find_target
private
def create_record(attrs)
attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
ensure_owner_is_not_new
record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) }
if block_given?
......@@ -212,6 +213,7 @@ def create_record(attrs)
end
def build_record(attrs)
attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash)
record = @reflection.klass.new(attrs)
if block_given?
add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) }
......
......@@ -1075,6 +1075,18 @@ def test_dependent_association_respects_optional_sanitized_conditions_on_delete
assert_equal 1, Client.find_all_by_client_of(firm.id).size
end
def test_creation_respects_hash_condition
ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
assert ms_client.save
assert_equal 'Microsoft', ms_client.name
another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create
assert !another_ms_client.new_record?
assert_equal 'Microsoft', another_ms_client.name
end
def test_dependent_delete_and_destroy_with_belongs_to
author_address = author_addresses(:david_address)
assert_equal [], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
......@@ -1887,6 +1899,18 @@ def test_create_by_new_record
assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
end
def test_creation_respects_hash_condition
post = categories(:general).post_with_conditions.build(:body => '')
assert post.save
assert_equal 'Yet Another Testing Title', post.title
another_post = categories(:general).post_with_conditions.create(:body => '')
assert !another_post.new_record?
assert_equal 'Yet Another Testing Title', another_post.title
end
def test_uniq_after_the_fact
developers(:jamis).projects << projects(:active_record)
developers(:jamis).projects << projects(:active_record)
......
......@@ -9,6 +9,9 @@ class Category < ActiveRecord::Base
:association_foreign_key => 'post_id',
:select => 'posts.*, 1 as correctness_marker')
has_and_belongs_to_many :post_with_conditions,
:class_name => 'Post',
:conditions => { :title => 'Yet Another Testing Title' }
def self.what_are_you
'a category...'
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册