Closed aomran closed 1 year ago
Perhaps an even simpler demonstration:
Given:
enumerize :status, :in => { active: 1, blocked: 2 }, scope: true
user = User.new
user.status = 1
user.attributes["status"]
=> 1
user.status = "blocked"
user.attributes["status"]
=> 2
Given (the default implementation ActiveRecord::Enum
or versions 2.2.2 or earlier of this gem):
enum status: { active: 1, blocked: 2 }
user = User.new
user.status = 1
user.attributes["status"]
=> "active"
user.status = "blocked"
user.attributes["status"]
=> "blocked"
Because values are type cast when they're set. The only reason this issue is related to reload
is because your custom implementation uses setters to set the value and enumerize doesn't implement a cast
method.
@aomran thanks! I merged it https://github.com/brainspec/enumerize/pull/424 here (so it doesn't include spec fix (I fixed it before))
How to reproduce (there's a spec that demonstrates this)
This is a regression from enumerize version 2.2.2. To be specific, this bug was introduced with this PR https://github.com/brainspec/enumerize/pull/321, when a reload method was defined.
It seems that Rails calls Type#cast and this gem has not defined that method. Defining cast resolves the issue. Type#deserialize by default calls Type#cast so removing that definition should be ok.