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.
This commit is contained in:
Holger Just 2012-08-20 18:01:55 +02:00
parent d24d4ce6b6
commit 7a4b664577
3 changed files with 15 additions and 1 deletions

View File

@ -41,7 +41,9 @@ class Document < ActiveRecord::Base
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

View File

@ -4,6 +4,7 @@ enumerations_001:
id: 1
type: DocumentCategory
active: true
is_default: true
enumerations_002:
name: User documentation
id: 2

View File

@ -40,6 +40,17 @@ class DocumentTest < ActiveSupport::TestCase
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?