2011-11-15 16 views
2

私はカラム名attributeを使用していくつかのテーブルで既存のdbを持っています。この名前を変更することはできません。アプリケーション全体を再コンパイルするという意味です。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にアクセスしてテーブルにアクセスしてください...

私の他の選択肢は何ですか - この小さなバグの周りに良い方法がありますか?

+0

おそらくこの質問の解決策はあなたを助けることができます:http://stackoverflow.com/questions/5428987/how-can-i-use-activerecord-on-a-database-that-has-a-column-named -attribute-d – JofoCodin

+0

これもあなたを助けることができます:http://stackoverflow.com/questions/4348037/overriding-or-aliasing-name-of-column-in-legacy-database-using-rails-activerecor – sparrovv

+0

I @radcheck = Radcheck.all(:select => 'attrとしての属性')。しかし、私はまだエラーが発生します:( –

答えて

3

これは完全に修正されていないため、私は自分自身に答えるつもりです。私のコントローラ内の列の名前を変更するにもかかわらず

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

私はまだ実際に属性を使用することができませんでした。

最後に、私はthis excellent gem, safe_attributes,を使ってこのトリックを行いました。ちょうどあなたのgemfileに宝石を追加し、宝石が自動的に名前の衝突の世話をする、のActiveRecordの予約済みの属性を気にせず

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

:今、私はこれを呼び出すことができます。

gem 'safe_attributes' 

はどのRailsの3.0にはActiveRecordによって予約されている属性だけで、あなたのGemfileに

gem 'safe_attributes' 

を追加気にすることなく、

3

..手すりを楽しみ、逸品の世話をしようとしますすべての名前が自動的に衝突します。

関連する問題