diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb index c763db04d7d..f6b9261984e 100644 --- a/app/models/concerns/has_custom_fields.rb +++ b/app/models/concerns/has_custom_fields.rb @@ -7,6 +7,12 @@ module HasCustomFields after_save :save_custom_fields end + def reload(options = nil) + @custom_fields = nil + @custom_fields_orig = nil + super + end + def custom_fields @custom_fields ||= refresh_custom_fields_from_db.dup end diff --git a/spec/components/concern/has_custom_fields_spec.rb b/spec/components/concern/has_custom_fields_spec.rb index 7f81dceb5ea..e37e9a72aa2 100644 --- a/spec/components/concern/has_custom_fields_spec.rb +++ b/spec/components/concern/has_custom_fields_spec.rb @@ -67,6 +67,26 @@ describe HasCustomFields do test_item.custom_fields["a"].should == "0" end + it "reload loads from database" do + test_item = CustomFieldsTestItem.new + test_item.custom_fields["a"] = 0 + + test_item.custom_fields["a"].should == 0 + test_item.save + + # should be casted right after saving + test_item.custom_fields["a"].should == "0" + + CustomFieldsTestItem.exec_sql("UPDATE custom_fields_test_item_custom_fields SET value='1' WHERE custom_fields_test_item_id=? AND name='a'", test_item.id) + + # still the same, did not load + test_item.custom_fields["a"].should == "0" + + # refresh loads from database + test_item.reload.custom_fields["a"].should == "1" + test_item.custom_fields["a"].should == "1" + + end it "double save actually saves" do test_item = CustomFieldsTestItem.new