Define collection singular ids method for has_many :through associations. Closes #8763.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7137 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
02311a5db1
commit
b5b16a80a9
@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Define collection singular ids method for has_many :through associations. #8763 [lifofifo]
|
||||
|
||||
* Array attribute conditions work with proxied association collections. #8318 [kamal, theamazingrando]
|
||||
|
||||
* Fix polymorphic has_one associations declared in an abstract class. #8638 [lifofifo, daxhuiberts]
|
||||
|
@ -674,6 +674,7 @@ def has_many(association_id, options = {}, &extension)
|
||||
|
||||
if options[:through]
|
||||
collection_reader_method(reflection, HasManyThroughAssociation)
|
||||
collection_accessor_methods(reflection, HasManyThroughAssociation, false)
|
||||
else
|
||||
add_multiple_associated_save_callbacks(reflection.name)
|
||||
add_association_callbacks(reflection.name, reflection.options)
|
||||
@ -1060,7 +1061,7 @@ def collection_reader_method(reflection, association_proxy_class)
|
||||
end
|
||||
end
|
||||
|
||||
def collection_accessor_methods(reflection, association_proxy_class)
|
||||
def collection_accessor_methods(reflection, association_proxy_class, writer = true)
|
||||
collection_reader_method(reflection, association_proxy_class)
|
||||
|
||||
define_method("#{reflection.name}=") do |new_value|
|
||||
@ -1077,7 +1078,7 @@ def collection_accessor_methods(reflection, association_proxy_class)
|
||||
define_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
|
||||
ids = (new_value || []).reject { |nid| nid.blank? }
|
||||
send("#{reflection.name}=", reflection.class_name.constantize.find(ids))
|
||||
end
|
||||
end if writer
|
||||
end
|
||||
|
||||
def add_multiple_associated_save_callbacks(association_name)
|
||||
|
@ -11,6 +11,7 @@
|
||||
require 'fixtures/category'
|
||||
require 'fixtures/post'
|
||||
require 'fixtures/author'
|
||||
require 'fixtures/comment'
|
||||
require 'fixtures/tag'
|
||||
require 'fixtures/tagging'
|
||||
|
||||
@ -420,7 +421,7 @@ def test_deprecated_inferred_foreign_key
|
||||
|
||||
class HasManyAssociationsTest < Test::Unit::TestCase
|
||||
fixtures :accounts, :companies, :developers, :projects,
|
||||
:developers_projects, :topics
|
||||
:developers_projects, :topics, :authors, :comments
|
||||
|
||||
def setup
|
||||
Client.destroyed_client_ids.clear
|
||||
@ -1014,14 +1015,21 @@ def test_assign_ids
|
||||
end
|
||||
|
||||
def test_assign_ids_ignoring_blanks
|
||||
firm = Firm.new("name" => "Apple")
|
||||
firm = Firm.create!(:name => 'Apple')
|
||||
firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
|
||||
firm.save
|
||||
firm.reload
|
||||
assert_equal 2, firm.clients.length
|
||||
firm.save!
|
||||
|
||||
assert_equal 2, firm.clients(true).size
|
||||
assert firm.clients.include?(companies(:second_client))
|
||||
end
|
||||
|
||||
def test_get_ids_for_through
|
||||
assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
|
||||
end
|
||||
|
||||
def test_assign_ids_for_through
|
||||
assert_raise(NoMethodError) { authors(:mary).comment_ids = [123] }
|
||||
end
|
||||
end
|
||||
|
||||
class BelongsToAssociationsTest < Test::Unit::TestCase
|
||||
|
Loading…
Reference in New Issue
Block a user