2011-11-15 2 views
2

난 컬럼 이름 attribute을 사용하여 일부 테이블과 기존의 데이터베이스있어. 우리의 전체 어플리케이션을 다시 컴파일한다는 의미에서이 이름을 바꿀 수는 없습니다. DB를 액세스하려고 할 때ActiveRecord :: DangerousAttributeError - 속성? ActiveRecord에 의해 정의됩니다

, 내가 끝낼 :

attribute? is defined by ActiveRecord 

첫 번째가 내가 datamapper를 사용하여 시도하지만 난 그것에서 얻을 수없는 자신이 파괴되어서는 안 물건을 고정 발견하고 - 같은 중첩 된 속성 .... 그래서

, 나는 다시 아칸소에 왔어요하고 문제를 해결하기 위해이를 사용하고 있습니다 :

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 

을하지만 그 혼란 그리고 때 실제로 그럴 내 주위를 어지럽히 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 => 'attribute as attr'). 하지만 여전히 오류가 발생합니다. –

답변

3

위의 내용이 완전히 수정되지 않았기 때문에 나는 스스로에게 대답 할 것입니다.

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

난 아직 실제로 속성을 사용할 수 없습니다 :

내 컨트롤러에서 열 이름을 변경에도 불구하고.

결국이 트릭을 수행하기 위해 this excellent gem, safe_attributes,을 사용했습니다. , 액티브의 예약 된 속성에 대한 걱정없이

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

을 당신의 gemfile에 보석을 추가하고 보석 자동으로 이름 충돌의 처리됩니다 : 지금은 이것으로 호출 할 수 있습니다.

gem 'safe_attributes' 

는 대한 레일 3.0에서 액티브가 보유 속성 당신의 Gemfile에

gem 'safe_attributes' 

를 추가 상관하지 않고

3

.. 난간을 즐길 보석은 알아서하려고합니다 충돌하는 모든 이름이 자동으로 표시됩니다.

관련 문제