ActiveRecord::ConnectionAdapters::Column
曾经有一个叫做type_cast
的方法,该方法接受一个字符串并将其“转换为适当的实例”。在某些时候似乎已将其删除,但我不知道该怎么做来替换它。
以下是使用它的代码:
# Create a column that will be responsible for typecasting
@column = ActiveRecord::ConnectionAdapters::Column.new(attribute.to_s, options[:default], @type == 'any' ? nil : @type)
# Typecasts the value based on the type of preference that was defined
def type_cast(value)
if @type == 'any'
value
else
@column.type_cast(value)
end
end
我正在使用Rails / ActiveRecord 4.2.10。
这里有一个列表:typecast alternatives,但据我所知并不是特别有用。
ETA:目前,我已经从原始的type_cast复制了代码,并对其进行了修改以在本地使用。但是,如果有一个真正的解决方案,我会更愿意。
答案 0 :(得分:0)
ActiveRecord::ConnectionAdapters::Column#type_cast
已被删除/移动到了API的其他部分,包括type_cast_for_database
或type_cast_for_schema
之类的东西。
您可以在连接上使用type_cast
,如下所示:
@column = ActiveRecord::ConnectionAdapters::Column.new(attribute.to_s, options[:default], @type == 'any' ? nil : @type)
# Typecasts the value based on the type of preference that was defined
def type_cast(value)
if @type == 'any'
value
else
ActiveRecord::Base.connection.type_cast(value, @column) # <---- HERE
end
end
如果您好奇的话,可以在_type_cast和type_cast处挖掘内部信息。