#< ActiveRecord :: ConnectionAdapters :: Column>的未定义方法'type_cast' (NoMethodError) [英] undefined method `type_cast' for #<ActiveRecord::ConnectionAdapters::Column> (NoMethodError)

查看:63
本文介绍了#< ActiveRecord :: ConnectionAdapters :: Column>的未定义方法'type_cast' (NoMethodError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ActiveRecord :: ConnectionAdapters :: Column 以前有一个名为 type_cast 的方法,该方法接收了一个字符串并将其强制转换到适当的情况。



这里是使用它的代码: p>

 #创建一个负责类型转换的列
@column = ActiveRecord :: ConnectionAdapters :: Column.new(attribute .to_s,options [:default],@type =='any'?nil:@type)

#根据定义的首选项类型强制转换值
def type_cast( value)
if @type =='any'
value
else
@ column.type_cast(value)
end
end

我正在使用Rails / ActiveRecord 4.2.10。



这里有一个列表: typecast替代方法,但是到目前为止,它并不是特别有用据我所知。



ETA:目前,我已经从原始的type_cast复制了代码,并对其进行了修改。 o在本地使用。

解决方案

ActiveRecord :: ConnectionAdapters ::列#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)

#根据定义的首选项类型转换值
def type_cast(value)
如果@type =='any'

否则
ActiveRecord :: Base.connection.type_cast(value,@column)#< ----这里
结束
结束

您可以在 _ type_cast type_cast 。。 p>

ActiveRecord::ConnectionAdapters::Column used to have a method called type_cast which took a string and cast it "to an appropriate instance". This appears to have been removed at some point, and I can't figure out what I should do to replace it.

Here is the code that uses it:

  # 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

I'm using Rails/ActiveRecord 4.2.10.

There's a list here: typecast alternatives, but it's not particularly useful, so far as I can tell.

ETA: For now, I've copied the code from the original type_cast and modified it to use it locally. But if there's a real solution, I'd prefer that.

解决方案

ActiveRecord::ConnectionAdapters::Column#type_cast was removed / moved to other parts of the API, including things like type_cast_for_database or type_cast_for_schema.

You can use the type_cast on the connection though, like this:

@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

You can dig in the internals at _type_cast and type_cast if you are curious.

这篇关于#&lt; ActiveRecord :: ConnectionAdapters :: Column&gt;的未定义方法'type_cast' (NoMethodError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆