html - rails alternative to enum and view integration -
rails doesn't offer enum
types, need data member can accept 5 values. moreover, want integrated automatically rails forms helper: select_tag
.
what's right solution situation?
p.s, i'd rather not use external plugins, if built-in , neat solution exist.
i keep functionality close it's used possible.
if values used single model, keep them in model, e.g., if users have possible types, , types, might like:
class user < activerecord::base types = %w{guest, paid, admin} # plus validation on `type` field. # maybe plus setter override validates. end
when need refer types elsewhere, allowable values in select:
user::types
there number of valuable tweaks around this, providing decorators make them "human readable" (capitalized, spaced, whatever) or metaprogramming methods allow things like:
user.is_guest? # or... user.make_guest! # or... user.guest!
i use own small gem functionality because it's case full-blown association , provides no value. allows things like:
class user < activerecord::base simple_enum :user_type, %w{guest, paid, admin} end
Comments
Post a Comment