-
Notifications
You must be signed in to change notification settings - Fork 71
Bitmask attributes are not reset when an object is reloaded #37
Comments
I've found the same problem and have constructed a test for it to add to the project that will currently fail: should "update bitmask values currently in the database with reload" do
instance1 = @campaign_class.create(:medium => [:web, :print])
instance2 = @campaign_class.find(instance1.id)
assert instance1.id == instance2.id
assert instance1.object_id != instance2.object_id
assert instance1.update_attributes(:medium => [:email])
assert_equal [:web, :print],instance2.medium
instance2.reload
assert_equal [:email],instance2.medium
end However, I'm not sure what the fix is. In reviewing the AR code, there doesn't appear to be a clear way to detect this situation and refresh the ValueProxy objects used to cache the current set of symbols corresponding to the integer values of the model. Any suggestions? |
So, to answer my own question: We could simply overload the "reload()" method for the model. That *should be okay. I'll work up a proposed solution and submit... |
…en overriding ActiveRecord::Base#reload and test it (issue #37)
fix for issue #37 where reloading a model doesn't refresh the ValueProxy objects
I found it is not compatible with included do
alias_method :reload_without_bitmasks, :reload
def reload(options = nil)
super_result = reload_without_bitmasks(options)
self.class.bitmasks.keys.each { |attribute| send("reload_#{attribute}") }
super_result
end
end And I think it should be auto included by ActiveRecord::Base, it should be included by explicit declaration as follow class Example < ActiveRecord::Base
extend BitmaskAttributes
# ...
end |
Unlike normal attributes, unsaved bitmask attributes are not reset when an object is reloaded. This was very surprising to me!
The text was updated successfully, but these errors were encountered: