Attribute accessor with a datatype
-
12-12-2019 - |
Question
I am looking to set an attribute_accessor with a datatype associated with it like how we have fields in the model that have database that have datatypes like string , integer etc associated with them.
How do i tell rails that my attr_accessor should respond to a specific datatype. I am looking to set a datetime to one the accessor i am trying to create,
Please suggest.
Solution
You can make a custom setter like
def field_name=(value)
# check for type and dont assign it if its the wrong typ
# or raise an error
end
just off of the top of my head
Also, duplicate question: attr_accessor strongly typed Ruby on Rails
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow