How do I find out the variable type of a certain field retrieved from the database in Rails / Ruby?
Posted on 2010-08-14
Hi, I have an application in Ruby on Rails which I need to find out what the column type is in the database through active record.
So for example, if I have a model Contact with the fields :name string and :total integer. In the model, the defaults created_at for timestamp and id will be there.
I would like to know how to find out based on a post from a form, what the particular column is.
So ... I have a form which posts a variable field_name through to an action in a controller.
In the controller, I've got an action ...
contact = Contact.find(:first)
field_name = params[:field_name]
# find out what the field_name's column type is in Contact model
if column_type(field_name) == 'timestamp'
# do something
elsif column_type(field_name) == 'integer'
# do something else
I hope you get the idea... it's like the mysql_field_type() in PHP.