aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/associations_test.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-10-01 19:15:51 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-10-01 19:15:51 +0000
commit0092d0ac6de041995d6f4555c6e52590799cbefb (patch)
treeee9c20184991e1226c79430c9527fa9086ad9757 /activerecord/test/associations_test.rb
parentf1d1f01b6d792b6e02bde2b5d73f833edc65d211 (diff)
downloadrails-0092d0ac6de041995d6f4555c6e52590799cbefb.tar.gz
rails-0092d0ac6de041995d6f4555c6e52590799cbefb.tar.bz2
rails-0092d0ac6de041995d6f4555c6e52590799cbefb.zip
Association collections have an _ids reader method to match the existing writer for collection_select convenience (e.g. employee.task_ids). The writer method skips blank ids so you can safely do @employee.task_ids = params[:tasks] without checking every time for an empty list or blank values. References #1887, Closes #5780.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5214 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/associations_test.rb')
-rwxr-xr-xactiverecord/test/associations_test.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index b843f5bdf5..a75f708ea7 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -934,6 +934,10 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert firm.clients.include?(Client.find_by_name("New Client"))
end
+ def test_get_ids
+ assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
+ end
+
def test_assign_ids
firm = Firm.new("name" => "Apple")
firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
@@ -942,6 +946,16 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal 2, firm.clients.length
assert firm.clients.include?(companies(:second_client))
end
+
+ def test_assign_ids_ignoring_blanks
+ firm = Firm.new("name" => "Apple")
+ firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
+ firm.save
+ firm.reload
+ assert_equal 2, firm.clients.length
+ assert firm.clients.include?(companies(:second_client))
+ end
+
end
class BelongsToAssociationsTest < Test::Unit::TestCase
@@ -1735,4 +1749,29 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL', :group => group.join(",")).size
end
+
+ def test_get_ids
+ assert_equal [projects(:active_record).id, projects(:action_controller).id], developers(:david).project_ids
+ assert_equal [projects(:active_record).id], developers(:jamis).project_ids
+ end
+
+ def test_assign_ids
+ developer = Developer.new("name" => "Joe")
+ developer.project_ids = [projects(:active_record).id, projects(:action_controller).id]
+ developer.save
+ developer.reload
+ assert_equal 2, developer.projects.length
+ assert_equal projects(:active_record), developer.projects[0]
+ assert_equal projects(:action_controller), developer.projects[1]
+ end
+
+ def test_assign_ids_ignoring_blanks
+ developer = Developer.new("name" => "Joe")
+ developer.project_ids = [projects(:active_record).id, nil, projects(:action_controller).id, '']
+ developer.save
+ developer.reload
+ assert_equal 2, developer.projects.length
+ assert_equal projects(:active_record), developer.projects[0]
+ assert_equal projects(:action_controller), developer.projects[1]
+ end
end