我已经有一些使用列名attribute
的表的现有数据库。我无法改变这个名字,因为这意味着重新编译我们的整个应用程序。ActiveRecord :: DangerousAttributeError - 属性?由ActiveRecord定义
当试图访问数据库,我结束了:
attribute? is defined by ActiveRecord
首先登场的我尝试使用DataMapper的,但我不能用它得到和我发现自己固定的东西不应该被打破 - 像嵌套属性....
所以,我回来Ar和正在使用该解决的问题:
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
但是,这是凌乱而乱搞我当我真正TR y并进入桌面...
我的其他选择是什么 - 有没有什么好的方法绕着这个小小的玩具?
也许这个问题的解决方案可以帮助你:http://stackoverflow.com/questions/5428987/how-can-i-use-activerecord-on-a-database-that-has-a-column-named -attribute-d – JofoCodin
这个也可以帮助你:http://stackoverflow.com/questions/4348037/overriding-or-aliasing-name-of-column-in-legacy-database-using-rails-activerecor – sparrovv
I以前试过这个:@radcheck = Radcheck.all(:select =>'attr'属性)。但我仍然得到一个错误:( –