1

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.

Raghu
  • 2,543
  • 1
  • 19
  • 24

1 Answers1

3

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

Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • attr_accessible and attr_accessor are two VERY different things and attr_accessible has got nothing to do with the question asked above. – Aditya Sanghi Jul 26 '12 at 18:32