Forum: Ruby on Rails detect column type?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-10 18:11
Using column_names I can get a nice list of columns in a table, but is
there any way to detect the column *type?*  Like "string", "integer",
"boolean", etc?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-10 18:15
(Received via mailing list)
On 10 Jan 2009, at 17:11, Taylor Strait
<rails-mailing-list@andreas-s.net
 > wrote:

>
> Using column_names I can get a nice list of columns in a table, but is
> there any way to detect the column *type?*  Like "string", "integer",
> "boolean", etc?

The columns method returns an array of column objects, which among
other things, has the name and type of the corresponding column.

Fred
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-10 18:35
Frederick Cheung wrote:
> On 10 Jan 2009, at 17:11, Taylor Strait
> <rails-mailing-list@andreas-s.net
>  > wrote:
>
>>
>> Using column_names I can get a nice list of columns in a table, but is
>> there any way to detect the column *type?*  Like "string", "integer",
>> "boolean", etc?
>
> The columns method returns an array of column objects, which among
> other things, has the name and type of the corresponding column.
>
> Fred

Indeed.  It returns the below for an id column.  Thanks.

#<ActiveRecord::ConnectionAdapters::SQLiteColumn:0x23736b8
@precision=nil, @primary=true, @default=nil, @limit=nil, @type=:integer,
@null=false, @name="id", @scale=nil, @sql_type="INTEGER">
This topic is locked and can not be replied to.