aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-06-27 08:19:12 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-06-27 08:19:12 +0000
commitb5b16a80a9e0386781b370cf110fdc26e13dbc61 (patch)
tree13d82a1de2c4c2b51368736ae9c0cece18a53747
parent02311a5db1800a4654f80ec71e8b7d32b444ff27 (diff)
downloadrails-b5b16a80a9e0386781b370cf110fdc26e13dbc61.tar.gz
rails-b5b16a80a9e0386781b370cf110fdc26e13dbc61.tar.bz2
rails-b5b16a80a9e0386781b370cf110fdc26e13dbc61.zip
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
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb5
-rwxr-xr-xactiverecord/test/associations_test.rb18
3 files changed, 18 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 886a6445da..156f55e94e 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -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]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index dd7464432a..ca739eb5dc 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -674,6 +674,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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)
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 93b1da10c6..bf6d8301a2 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -11,6 +11,7 @@ require 'fixtures/categorization'
require 'fixtures/category'
require 'fixtures/post'
require 'fixtures/author'
+require 'fixtures/comment'
require 'fixtures/tag'
require 'fixtures/tagging'
@@ -420,7 +421,7 @@ end
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 @@ class HasManyAssociationsTest < Test::Unit::TestCase
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