2011-04-27 95 views
2

擴展模式,我有以下型號設置: 回報率:ActiveRecord的,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?

產生

 
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提供的'錯誤'對象,解決方案是用更具體的名稱重命名關聯 – Mukund 2011-04-27 19:25:40

回答

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

此聲明覆蓋誤差協會/對象的ActiveRecord提供,因此我們通過寬鬆的ActiveRecord ::驗證提供的所有驗證功能。將關聯重命名爲更具體的事情可以解決問題。

正確執行類:

 
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 :: Error錯誤重命名爲Qa :: TransactionError是可選的。我只是這麼做的,所以我的命名約定在整個應用程序中都是一致的。