VARCHAR类型被错误地转换为ActiveRecord :: Type :: Value或ActiveRecord :: Type :: Decimal with mysql adapter

时间:2017-04-12 14:26:47

标签: mysql ruby activerecord

以下是我最终使用错误的ActiveRecord类型的方法。

首先,表格的架构:

Field       | Type
------------------------------
id          | int(10) unsigned
user_name   | varchar(255)

以下是我在irb中尝试的代码:

require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'mysql', host: 'mrom.akamai.com', username: 'SOME_USER', password: 'SOME_PASSWORD', database: 'mrom_v3')
c=ActiveRecord::Base.connection
r = c.exec_query("select id, user_name, 'fixed string' value from user limit 2")
t = r.column_types

=> {"id"=>#<ActiveRecord::Type::Integer:0x007fc144a5ed00 @precision=nil, @scale=nil, @limit=nil, @range=-2147483648...2147483648>, "user_name"=>#<ActiveRecord::Type::Value:0x007fc144113f20 @precision=nil, @scale=nil, @limit=nil>, "value"=>#<ActiveRecord::Type::Decimal:0x007fc144113e30 @precision=nil, @scale=nil, @limit=nil>} 

注意user_name和value列的:: Value和:: Decimal类型。我期待着String。

我该如何解决这个问题?

0 个答案:

没有答案