views:

76

answers:

1

How do you check what the datatype is for something that was retrieved from the database?

For example, if I have some instantiation of a model @model with a database field "title", I want to be able to code something like @model.title.type and have it return "String". Does Rails have any built-in functionality for this?

+4  A: 

Try this:

@model.column_for_attribute('title').type

Should return :string, :text, :integer, etc.

The ActiveRecord Column class also includes a number of other attributes: default, limit, name, null, precision, primary, scale, sql_type, type.

Dave Ray
Note also has: name, precision, primary, null, limit and default
Sam Saffron
Thanks, that did the trick.
Karl