Get database type of ActiveRecord object attribute

Let's say I have this in schema.rb:

create_table "products" do |t|
 t.string   "name",                                           
 t.text     "description"

I want a way to find out what the database type of a product attribute is.

Something like: Product.column_type(:name) => "string"

Is this possible?


Product.columns_hash['name'].type # => :string

Note the using of strings instead of symbols.

About an alternative: sql_type

It maps logical Rails types to DB-specific data types. For general purpose things I wouldn't recommend using it: your production DB-engine will return the value which won't be the same as the sql_type for the very same column in your development DB (although they both were created with the same migration file). Example for boolean field:

# SQLite mapping:
:boolean => { :name => "boolean" }
# MySQL mapping:
:boolean => { :name => "tinyint", :limit => 1 }

Using type in both cases give you :boolean, as you specify in migrations.

Need Your Help

How do I open a new JFrame and run my game after closing another JFrame?

java jframe minesweeper

I'm making a minesweeper game, and I want the user to be able to pick from beginner, intermediate, and advanced before playing the game. So far what I have is a JFrame that opens when I open the pr...

WP8 app with large signalr component only works on Emulator

azure windows-phone-8 signalr

I have a SignalR enabled Web API server on Azure, and I have created a Windows Phone 8 client app that works perfectly well with the server on my WP8 Emulator.