2011-04-27 6 views
2

에서 확장하는 모델에서 나는 다음과 같은 모델을 설정 한 : 는 RoR에 : 액티브, NoMethodError AR

 
class Qa::Base < ActiveRecord::Base 
    self.abstract_class = true 
    Qa::Base.establish_connection("qa_audit_#{RAILS_ENV}") 
end

class Qa::ErrorType < Qa::Base set_table_name "error_types"
# Associations has_many :errors, :class_name => 'Qa::Error' has_many :evaluations, :class_name => 'Qa::Evaluation', :through => :errors
# Validations validates_presence_of :content validates_uniqueness_of :content end

그러나 저장할 때/나는 다음과 같은 NoMethodErrors 타격 유지 모델을 검증 :

NoMethodError (undefined method `add_on_blank' for #Class:0x23a3020):

예 :

 
e = Qa::ErrorType.first
e.valid?

012,351,641 생산

 
NoMethodError: undefined method add_on_blank' for #<Class:0x223eeb4> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1994:inmethod_missing_without_paginate' 
    from /opt/local/lib/ruby/gems/1.8/gems/will_paginate-2.3.14/lib/will_paginate/finder.rb:170:in method_missing' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:insend' 
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in method_missing_without_paginate' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2178:inwith_scope' 
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:in send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:inwith_scope' 
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:376:in method_missing_without_paginate' from /opt/local/lib/ruby/gems/1.8/gems/will_paginate-2.3.14/lib/will_paginate/finder.rb:170:inmethod_missing' 
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:599:in validates_presence_of' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:incall' 
    from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:in evaluate_method' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:166:incall' 
    from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in run' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:ineach' 
    from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:in send' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:inrun' 
    from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:276:in run_callbacks' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1110:invalid_without_callbacks?' 
    from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:315:in `valid?' 

이전에 같은 응용 프로그램에서 다른 코드 패턴을 사용했지만 그 부분은 여전히 ​​유효합니다 (모든 유효성 검사는 예상대로 작동 함).

내가 뭘 잘못하고 있는지 누군가가 밝힐 수 있습니까?

+1

문제는 연관 has_many : ActiveRecord :: Validations에서 제공 한 'errors'객체를 오버라이드하는 것이 었습니다. 더 구체적인 이름으로 연결 이름을 바꾸는 것이 해결책이었습니다. – Mukund

답변

4

문제가 무엇인지 파악 :

 
class Qa::ErrorType < Qa::Base 
    set_table_name "error_types"
# Associations has_many :errors, :class_name => 'Qa::Error' has_many :evaluations, :class_name => 'Qa::Evaluation', :through => :errors
# Validations validates_presence_of :content validates_uniqueness_of :content end

이 선언은 액티브 우리가 액티브 ::되는 검증에서 제공하는 모든 유효성 검사 기능을 잃어 따라서 제공하는 오류 협회/객체를 무시합니다. 연결을보다 구체적인 것으로 변경하면 문제가 해결됩니다. 클래스의

올바른 구현 : 그들은이 변경 이후에 의미로

 
class Qa::ErrorType < Qa::Base 
    set_table_name "error_types"
# Associations has_many :transaction_errors, :class_name => 'Qa::TransactionError' has_many :evaluations, :class_name => 'Qa::Evaluation', :through => :transaction_errors
# Validations validates_presence_of :content validates_uniqueness_of :content end

모든 검증이 작동합니다. Qa :: TransactionError 클래스의 이름을 바꾸는 것은 선택 사항입니다. 방금 했으므로 명명 규칙이 응용 프로그램 전체에서 일관됩니다.