如何在具有名为“valid”列的数据库上使用 ActiveRecord? (危险属性错误)

发布于 2024-08-06 01:01:28 字数 701 浏览 3 评论 0原文

我正在访问一个无法更改的数据库,并且它定义了一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爺獨霸怡葒院 2024-08-13 01:01:28

试试这个:

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 中不起作用,但它现在可以解决这个问题。

我在 ruby on Rails 邮件列表 上找到了它,

如果您愿意,您还可以查看datamapper,它可以更理智地处理此类事情。

Try this:

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

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.

一向肩并 2024-08-13 01:01:28

使用 safe_attributes - https://github.com/bjones/safe_attributes 。它开箱即用,完美运行:

class WebsiteUser < ActiveRecord::Base
    establish_connection 'cf_website'
    set_table_name 'nc_users'
    bad_attribute_names :hash    
end

Use safe_attributes - https://github.com/bjones/safe_attributes . It works perfectly out of the box:

class WebsiteUser < ActiveRecord::Base
    establish_connection 'cf_website'
    set_table_name 'nc_users'
    bad_attribute_names :hash    
end
哆兒滾 2024-08-13 01:01:28

不用担心 ActiveRecord 的保留属性,只需在 gemfile 中添加一个 gem,该 gem 就会自动处理名称冲突。

gem 'safe_attributes'

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.

gem 'safe_attributes'

http://goo.gl/OO2H7

ˇ宁静的妩媚 2024-08-13 01:01:28

对于读取,您可以使用 SQL 的 select-as 语句。不确定以下内容是否有效,但默认范围可能会使此操作变得容易。

class MyRecord < ActiveRecord::Base
    default_scope :select=> 'valid as valid_column'
end

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.

class MyRecord < ActiveRecord::Base
    default_scope :select=> 'valid as valid_column'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文