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.
Taylor S. (Guest)
on 2009-01-10 19: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?
Frederick C. (Guest)
on 2009-01-10 19:15
(Received via mailing list)
On 10 Jan 2009, at 17:11, Taylor S.
<removed_email_address@domain.invalid
 > 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
Taylor S. (Guest)
on 2009-01-10 19:35
Frederick C. wrote:
> On 10 Jan 2009, at 17:11, Taylor S.
> <removed_email_address@domain.invalid
>  > 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.