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

发布于 2024-10-25 18:21:42 字数 937 浏览 3 评论 0原文

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

attribute?由 ActiveRecord(ActiveRecord::DangerousAttributeError) 定义

我的代码:

class User < ActiveRecord::Base
      def self.authenticate(username,password)
        where(:username => username, :value => password).first
      end
end

我在 ruby​​ on Rails 邮件列表上找到了一个解决问题的计划,但对我不起作用

  class << self
    def instance_method_already_implemented?(method_name)
      return true if method_name == 'attribute'
      super
    end
  end

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

  • ruby​​ 1.9.2p0 (2010-08-18 修订版 29036)[x86_64-darwin10.4.0]
  • Rails
  • 3.0.1 activerecord(3.0.1)activeresource(3.0.1)

更新(已解决):

计划A:

select("username, value").where(:username => username, :value => password).first

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

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

my code:

class User < ActiveRecord::Base
      def self.authenticate(username,password)
        where(:username => username, :value => password).first
      end
end

I found a plan on the ruby on rails mailing list for fix the problem but not work for me

  class << self
    def instance_method_already_implemented?(method_name)
      return true if method_name == 'attribute'
      super
    end
  end

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

  • ruby 1.9.2p0 (2010-08-18 revision
    29036) [x86_64-darwin10.4.0]
  • Rails
  • 3.0.1 activerecord (3.0.1) activeresource (3.0.1)

UPDATE(solved):

PLAN A:

select("username, value").where(:username => username, :value => password).first

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

不必了 2024-11-01 18:21:42

ActiveRecord 查询支​​持 :select 参数,该参数允许您定义想要以字符串形式返回的字段。

通常人们使用类似的内容:

:select => 'field1, field2'

如果您知道数据库服务器的原始查询语言,那么您可以在选择字符串中使用它。使用 SQL 选择字段时的选项之一是使用 as 修饰符:

select field1 as the_first_field, field2 as the_second_field

数据库将使用新字段名称而不是旧字段名称返回字段。如果您的数据库支持,那么这是一种管理遗留字段的简单方法,这些字段的命名方式与 Rails 冲突。

请参阅“五个 ActiveRecord 技巧”中的“学习喜爱 ActiveRecord 的 :select 参数”和“选择特定字段”。

以下是我的一个 Rails 应用程序使用 rails 控制台 访问 Postgres 数据库的示例:

ruby-1.9.2-p180 :007 > dn = DomainName.first
 => #<DomainName id: 1, domain_name: "ip72-208-155-230.ph.ph.cox.net", created_at: "2010-04-20 05:53:22", updated_at: "2010-04-20 05:53:22"> 
ruby-1.9.2-p180 :008 > dn = DomainName.first(:select => 'id, domain_name as dn')
 => #<DomainName id: 1> 
ruby-1.9.2-p180 :009 > dn['id']
 => 1 
ruby-1.9.2-p180 :010 > dn['dn']
 => "ip72-208-155-230.ph.ph.cox.net"

ActiveRecord queries support the :select parameter, which lets you define the fields you want returned to you as a string.

Usually people use something like:

:select => 'field1, field2'

If you know the raw query language for your database server, then you can use that in the select string. One of the options when selecting fields using SQL is to use the as modifier:

select field1 as the_first_field, field2 as the_second_field

and the database will return the fields using the new field names instead of the old field names. It's an easy way to manage legacy fields that are named in ways that conflict with Rails if your database supports that.

See "Learn to Love ActiveRecord's :select Parameter" in "Five ActiveRecord Tips" and "Selecting Specific Fields" in the Ruby on Rails Guides.

Here's an example from one of my Rails apps using the rails console to access my Postgres DB:

ruby-1.9.2-p180 :007 > dn = DomainName.first
 => #<DomainName id: 1, domain_name: "ip72-208-155-230.ph.ph.cox.net", created_at: "2010-04-20 05:53:22", updated_at: "2010-04-20 05:53:22"> 
ruby-1.9.2-p180 :008 > dn = DomainName.first(:select => 'id, domain_name as dn')
 => #<DomainName id: 1> 
ruby-1.9.2-p180 :009 > dn['id']
 => 1 
ruby-1.9.2-p180 :010 > dn['dn']
 => "ip72-208-155-230.ph.ph.cox.net"
倾其所爱 2024-11-01 18:21:42

对于列 x,ActiveRecord 创建 xx=x? 方法。因此,将您的修复更新为:

class << self
    def instance_method_already_implemented?(method_name)
        return true if method_name == 'attribute?'
        super
    end
end

For column x, ActiveRecord creates x, x=, and x? methods. So, update your fix to this:

class << self
    def instance_method_already_implemented?(method_name)
        return true if method_name == 'attribute?'
        super
    end
end
冷夜 2024-11-01 18:21:42

也许您可以使用 alias_attribute ,从而使用具有不同名称的方法。

Maybe you can use alias_attribute and thus use methods with different names.

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