我如何使用ActiveRecord上已经列名为'属性'数据库? (DangerousAttributeError) [英] How can I use ActiveRecord on a database that has a column named 'attribute'? (DangerousAttributeError)

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

问题描述

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

属性?由ActiveRecord的定义(的ActiveRecord :: DangerousAttributeError)

我的code:

 类用户的LT;的ActiveRecord :: Base的
      高清self.authenticate(用户名,密码)
        其中,(:用户名=>用户名:​​价值=>密码)。首先
      结束
结束
 

我发现红宝石计划在轨道上的邮件列表解决这个问题,但对我不起作用

 类<<自
    高清instance_method_already_implemented?(方法_)
      返回true,如果方法_ =='属性'
      超
    结束
  结束
 

我不知道,如果它的问题,但这里有我的环境的详细信息:

  • 红宝石1.9.2p0(2010-08-18修订 29036)的x86_64-darwin10.4.0]
  • 的Rails
  • 在3.0.1的ActiveRecord(3.0.1)的ActiveResource(3.0.1)

UPDATE(解决):

A计划:

 选择(用户名,值),其中(:用户名=>用户名:​​价值=>密码)。首先
 

解决方案

ActiveRecord的查询支持:选择参数,它可以让你定义你想回到你的领域一个字符串。

通常情况下人们使用这样的:

 :选择=> FIELD1,FIELD2
 

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

 选择字段1为the_first_field,FIELD2为the_second_field
 

和数据库将返回采用新的字段名称,而不是旧的字段名的字段。这是一个简单的方法来管理命名的方式,使用Rails冲突遗留的领域,如果你的数据库支持。

请参阅学会爱的ActiveRecord的:选择参数,在五ActiveRecord的提示选择特定的字段​​在Ruby on Rails的指南。

下面是从我的Rails的一个非常的应用程序使用轨控制台来访问我的Postgres数据库的例子:

 红宝石-1.9.2-P180:007> DN = DomainName.first
 => #<域名ID:1,DOMAIN_NAME:ip72-208-155-230.ph.ph.cox.net,created_at:2010-04-20 5时53分22秒,的updated_at:2010-04- 20 5时53分22秒>
红宝石-1.9.2-P180:008> DN = DomainName.first(:选择=>'身份证,DOMAIN_NAME为DN)
 => #<域名ID:1>
红宝石-1.9.2-P180:009> DN ['身份证']
 => 1
红宝石-1.9.2-P180:010> DN ['DN']
 => ip72-208-155-230.ph.ph.cox.net
 

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

解决方案

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"

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

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