ActiveRecord::DangerousAttributeError [英] ActiveRecord::DangerousAttributeError

查看:17
本文介绍了ActiveRecord::DangerousAttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须连接到我们的 freeradius 服务器使用的现有数据库.一个表有一个名为属性的列,我正在尝试访问该列.

I have to connect to an existing database used by our freeradius server. One table has a column called attribute which I'm trying to access.

访问时,我得到他的错误:

When accessing, I get his error:

ActiveRecord::DangerousAttributeError 
attribute? is defined by ActiveRecord

我尝试在我的模型中选择并重命名此列:

I've tried to select and rename this column in my model:

def self.default_scope
    Radcheck.select("attribute as newattribute")
end

但这也行不通.

有人可以推荐一种解决方法吗?我真的很想重命名 rails 中的列!

Can anyone recommend a way around this? I'd really like to rename the column in rails!

推荐答案

在一个类似的问题上我找到了这个答案:https://stackoverflow.com/a/9106597/1266906

On a similar question I found this answer: https://stackoverflow.com/a/9106597/1266906

无需关心在 Rails 3.0 中 ActiveRecord 保留了哪些属性,只需添加

Without having to care about which attributes are reserved by ActiveRecord in Rails 3.0 just add

gem 'safe_attributes'

到您的Gemfile,gem 会尝试自动处理所有冲突的名称.

to your Gemfile and the gem will try to take care of all colliding names automatically.

与其他答案一样,您需要使用 Radcheck[:attribute]Radcheck.read_attribute :attribute/Radcheck.write_attribute :attribute, 'value' 访问具有内部保留名称的字段,但 gem 确保像 validates_presence_of :attribute 这样的验证将照常工作.

As with the other answer you need to use Radcheck[:attribute] or Radcheck.read_attribute :attribute / Radcheck.write_attribute :attribute, 'value' to access fields with internally reserved names, but the gem ensures validations like validates_presence_of :attribute will work as usual.

更多详细信息,请访问 https://github.com/bjones/safe_attributes

Further details are available at https://github.com/bjones/safe_attributes

这篇关于ActiveRecord::DangerousAttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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