我如何使用ActiveRecord上已经列名为“有效”的数据库? (DangerousAttributeError) [英] How can I use ActiveRecord on a database that has a column named 'valid'? (DangerousAttributeError)

查看:211
本文介绍了我如何使用ActiveRecord上已经列名为“有效”的数据库? (DangerousAttributeError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我访问,我不能改变一个数据库,它有一个名为列的有效的定义。每当我尝试访问的属性,我得到这个异​​常:

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适配器(1.0.0.9250)
  • 红宝石的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

这是一个黑客,它可能无法在轨道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.

这篇关于我如何使用ActiveRecord上已经列名为“有效”的数据库? (DangerousAttributeError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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