Browse files

Fixed bug where setting a belongs_to association to nil would raise a…

…n error.
  • Loading branch information...
1 parent c67bf4b commit 605d903a25d25b21c4225100fdc3442f446b7307 Tyler Rick committed May 8, 2012
Showing with 8 additions and 0 deletions.
  1. +1 −0 lib/composite_primary_keys/attribute_methods/write.rb
  2. +7 −0 test/test_associations.rb
View
1 lib/composite_primary_keys/attribute_methods/write.rb
@@ -4,6 +4,7 @@ module Write
def write_attribute(attr_name, value)
# CPK
if attr_name.kind_of?(Array)
+ value = [nil]*attr_name.length if value.nil?
unless value.length == attr_name.length
raise "Number of attr_names and values do not match"
end
View
7 test/test_associations.rb
@@ -176,6 +176,13 @@ def test_composite_belongs_to_changes
assert_equal({:department_id=>[1, 2]}, steve.changes)
end
+ def test_composite_belongs_to__setting_to_nil
+ room_assignment = room_assignments(:jacksons_room)
+ # This was raising an error before:
+ # NoMethodError: undefined method `length' for nil:NilClass
+ assert_nothing_raised { room_assignment.room = nil }
+ end
+
def test_has_one_with_composite
# In this case a regular model has_one composite model
department = departments(:engineering)

0 comments on commit 605d903

Please sign in to comment.