-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Protection from passing wrong symbols/strings to enums. #96
Comments
maybe we just want |
You can use the whiny accessor, that works like that class User < ActiveRecord::Base
as_enum :gender, %w{male female}, accessor: :whiny
end
User.new(gender: "dunno") # => raises ArgumentError |
and you are welcome :) |
@lwe Yes, whiny accessor works great, but image we write a scope -> { where(state_cd: self.states[:wrong_state]) } It will silently paste Thank you for writing such a good gem :-) |
Ah, now I get it, sorry. Yes, please, feel free to provide a pull request with a |
@lwe ok! |
Done! #104 |
Hey, thanks! |
FYI, I'm closing this issue now, ok? |
Ok, thanks you! :-) |
Hey, @mainameiz
class ModelA <AR
as_enum :settlement_status, [:initiated, :settlementInProgress, :settled, :failure, :queuedInNextCycle], map: :string, source: :settlementStatus, accessor: :whiny
alias_attribute :settlement_status, :settlementStatus
end
class ModelB < AR
validates :status, presence: true
as_enum :status, [:active, :expired], map: :string, source: :status, accessor: :whiny
end When making new object of ModelB with invalid value for status, it throws error, but does nothing for ModelA, it creates the object and commits it to the database for example @model_a = ModelA.new(settlement_status: "abc")
@model_a.save it is committed successfully for @model_b = ModelB.new(status: "abc")
ArgumentError: abc is not a valid enum value for status I also tried validating presence of |
First of all, thank you for writing such a good gem. :-)
It seems to be better to raise an exception in such cases.
The text was updated successfully, but these errors were encountered: