← all posts

14 December 2008

Finding SQL Type of Column

exlibris asked tonight in #rubyonrails how to find the type of a column in the table for a model. I didn’t know this and maybe someone else out there may not know how either. The solution lies within the columns_hash method from ActiveRecord:Base which returns a hash containing all kinds of useful information about all the columns in your table such as:

That final column is the magic attribute we’re looking for. To get to it we access it like Model.columns_hash[“attribute”].sql_type

— Ryan

Got a thought? Email me or find me on Mastodon.

Before this: Gentlemen, (re)Start Your Engines!

After this: Moving on up