如何在具有名为“valid"的列的数据库上使用 ActiveRecord?(危险属性错误) [英] How can I use ActiveRecord on a database that has a column named 'valid'? (DangerousAttributeError)

查看:18
本文介绍了如何在具有名为“valid"的列的数据库上使用 ActiveRecord?(危险属性错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在访问一个无法更改的数据库,它定义了一个名为 valid 的列.每当我尝试访问属性时,都会遇到此异常:

I am accessing a database that I can't change and it has a column named valid defined. Anytime I try to access an attribute, I get this exception:

有效吗?由 ActiveRecord 定义(ActiveRecord::DangerousAttributeError)

valid? is defined by ActiveRecord (ActiveRecord::DangerousAttributeError)

异常是有道理的,但由于我无法更改数据库,我该如何解决此错误?

The exception makes sense, but since I'm not able to change the database, how can I get around this error?

我尝试覆盖"该属性,但我不知道如何删除原始列.我可以成功调用这个 valid_column 方法,但是每当我尝试访问数据库中定义的另一个属性时,都会遇到相同的异常.它似乎仍在尝试映射有效列.

I tried "overriding" the attribute, but I don't know how to remove the original column. I can successfully call this valid_column method, but any time I try to access another attribute defined in the database, I get the same exception. It still seems to be trying to map the valid column.

  def valid_column=(valid)
    write_attribute(:valid, valid)
  end
  def valid_column
      read_attribute(:valid)
  end

我不确定这是否重要,但以下是我的环境的详细信息:

I'm not sure if it matters, but here are the details of my environment:

  • Windows Ruby 1.8.6
  • Linux 服务器上的 Informix 数据库
  • activerecord (2.3.4)
  • activerecord-informix-adapter (1.0.0.9250)
  • ruby-informix (0.7.1)

提前致谢!

推荐答案

试试这个:

class MyTable < AR:Base
   class << self
     def instance_method_already_implemented?(method_name)
       return true if method_name == 'valid'
       super
     end
   end
end

这是一个 hack,它可能在 rails 3 中不起作用,但它现在可以解决这个问题.

It's a hack, and it might not work in rails 3, but it could fix the problem for now.

我在 ruby on rails 邮件列表

如果你愿意,你也可以看看 datamapper,它处理这些事情稍微理智一些.

If you wanted, you could also look at datamapper, which handles these sort of things somewhat more sanely.

这篇关于如何在具有名为“valid"的列的数据库上使用 ActiveRecord?(危险属性错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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