Commits

Anonymous committed daa6bfe

Set default category_id instead of the object #1087

Rails 2.3 still has issues with synchronizing the association_id
and association attributes of an object. That means, if you set the
association with an object first and then just set the id afterwards, the object wins and the setting of the id gets lost.

This is not an issue in Rails >= 3.1 anymore.

Comments (0)

Files changed (3)

app/models/document.rb

 
   def after_initialize
     if new_record?
-      self.category ||= DocumentCategory.default
+      # FIXME: on Rails 3 use this instead
+      # self.category ||= DocumentCategory.default
+      self.category_id = DocumentCategory.default.id if self.category_id == 0
     end
   end
 

test/fixtures/enumerations.yml

   id: 1
   type: DocumentCategory
   active: true
+  is_default: true
 enumerations_002:
   name: User documentation
   id: 2

test/unit/document_test.rb

     assert doc.save
   end
 
+  def test_build_with_category
+    category = Enumeration.find_by_name('User documentation')
+
+    doc = Project.find(1).documents.build
+    doc.safe_attributes = {:category_id => category.id}
+
+    # https://www.chiliproject.org/issues/1087
+    assert_equal category.id, doc.category_id
+    assert_equal category, doc.category
+  end
+
   def test_updated_on_with_attachments
     d = Document.find(1)
     assert d.attachments.any?