How to find out the type of a model's attribute

Posted . Visible to the public.

When you want to find out the data type of an attribute, you can just use ActiveRecord's columns_hash method.

It returns a hash of column objects that include a type attribute (and more database-related information).

Example:

Contract.columns_hash['id'].type
 => :integer
Contract.columns_hash['active'].type
 => :boolean
Contract.columns_hash['updated_at'].type
 => :datetime
Arne Hartherz
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2013-11-28 10:42)