如何在具有名为“valid”列的数据库上使用 ActiveRecord? (危险属性错误)
我正在访问一个无法更改的数据库,并且它定义了一个名为 valid 的列。每当我尝试访问属性时,都会收到此异常:
有效吗?由 ActiveRecord 定义 (ActiveRecord::DangerousAttributeError)
该异常是有道理的,但由于我无法更改数据库,如何解决此错误?
我尝试“覆盖”该属性,但我不知道如何删除原始列。我可以成功调用此 valid_column 方法,但每当我尝试访问数据库中定义的另一个属性时,都会遇到相同的异常。它似乎仍在尝试映射有效列。
def valid_column=(valid)
write_attribute(:valid, valid)
end
def valid_column
read_attribute(:valid)
end
我不确定这是否重要,但以下是我的环境的详细信息:
- Windows Ruby 1.8.6
- Linux 服务器上的
- Informix 数据库activerecord (2.3.4)
- activerecord-informix-adapter (1.0.0.9250)
- ruby-informix (0.7 .1)
提前致谢!
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:
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?
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
- Informix database on a Linux server
- activerecord (2.3.4)
- activerecord-informix-adapter (1.0.0.9250)
- ruby-informix (0.7.1)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
这是一个 hack,它可能在 Rails 3 中不起作用,但它现在可以解决这个问题。
我在 ruby on Rails 邮件列表 上找到了它,
如果您愿意,您还可以查看datamapper,它可以更理智地处理此类事情。
Try this:
It's a hack, and it might not work in rails 3, but it could fix the problem for now.
I found it on the ruby on rails mailing list
If you wanted, you could also look at datamapper, which handles these sort of things somewhat more sanely.
使用 safe_attributes - https://github.com/bjones/safe_attributes 。它开箱即用,完美运行:
Use safe_attributes - https://github.com/bjones/safe_attributes . It works perfectly out of the box:
不用担心 ActiveRecord 的保留属性,只需在 gemfile 中添加一个 gem,该 gem 就会自动处理名称冲突。
http://goo.gl/OO2H7
Without worrying about ActiveRecord's reserved attributes, just add a gem in your gemfile and the gem will take care of name collisions automatically.
http://goo.gl/OO2H7
对于读取,您可以使用 SQL 的 select-as 语句。不确定以下内容是否有效,但默认范围可能会使此操作变得容易。
For reads you might be able to use SQL's select-as statement. Not sure if the following will work, but a default scope may make this easily do-able.