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。
这里有一个列表:类型转换的替代方案 https://apidock.com/rails/ActiveRecord/ConnectionAdapters/Column/type_cast,但据我所知,它并不是特别有用。
ETA:目前,我已从原始 type_cast 复制了代码并对其进行了修改以在本地使用。但如果有一个真正的解决方案,我更愿意这样做。
ActiveRecord::ConnectionAdapters::Column#type_cast
被删除/移动到 API 的其他部分,包括诸如type_cast_for_database
or 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 https://apidock.com/rails/v4.2.7/ActiveRecord/ConnectionAdapters/Quoting/_type_cast and 类型转换 https://apidock.com/rails/v4.2.7/ActiveRecord/ConnectionAdapters/Quoting/type_cast如果你好奇的话。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)