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

问题描述

我有使用的列名属性一些表的现有数据库。我根本无法改变这个名字,因为这将意味着重新编译了整个应用程序。

I've got an existing db with some tables using the column name attribute. I simply cannot change this name as it would mean recompiling our whole application.

当试图访问该数据库,我结束了:

When trying to access the db, I end up with:

attribute? is defined by ActiveRecord

首先登场的我试着使用DataMapper的,但我不能得到它,我发现自己修理东西不应该被打破 - 像嵌套属性....

First up I tried using datamapper but I can't get on with it and am finding myself fixing things which shouldn't be broken - like nested attributes....

所以,我回来Ar和正在使用此方法解决问题:

So, I've come back to ar and am using this to solve the issues:

class Radcheck < ActiveRecord::Base
  set_table_name 'radcheck'
  class << self
       def instance_method_already_implemented?(method_name)
         return true if method_name == 'attribute?'
         return true if method_name == 'attribute_before_type_cast'
         return true if method_name == 'attribute='
         return true if method_name == 'attribute'
         return true if method_name == 'attribute_changed?'
         return true if method_name == 'attribute_change'
         return true if method_name == 'attribute_will_change!'
         return true if method_name == 'attribute_was'
         return true if method_name == 'attribute_column'
         return true if method_name == 'reset_attribute!'
         super
       end
   end
end

但是,这是混乱的,是搞乱我身边时,我居然试图访问表...

But that's messy and is messing me around when I actually try and access the table...

我有哪些其他的选择 - 是否有关于这个小鸡奸什么好办法

What are my other choices - are there any good ways around this little bugger?

推荐答案

我要回答这个自己因为上面并没有完全解决。

I'm going to answer this myself because the above didn't fix totally.

尽管重命名列在我的控制器:

Despite renaming the column in my controller:

 @radcheck = Radcheck.find(:all, :select => 'attribute AS attr')

我还是没能真正使用的属性。

I still couldn't actually use attribute.

在最后,我用这个优秀的宝石,safe_attributes,以这样的伎俩。现在我可以这样调用:

In the end, I used this excellent gem, safe_attributes, to do the trick. Now I can call with this:

<% @radcheck.each do |radcheck| %>
<%= radcheck.attr %>
<% end %>

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

10-21 14:18