RoR:ActiveRecord,NoMethodError在从AR扩展的模型中
我建立了以下模型:
class Qa::Base < ActiveRecord::Base self.abstract_class = true Qa::Base.establish_connection("qa_audit_#{RAILS_ENV}") endclass 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 methodadd_on_blank' for #<Class:0x223eeb4> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1994: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/associations/association_collection.rb:380:in
send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:inmethod_missing_without_paginate' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2178:in
with_scope' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:insend' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:in
with_scope' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:376: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/validations.rb:599:invalidates_presence_of' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:in
call' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:182:inevaluate_method' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:166:in
call' 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:90:in
each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/callbacks.rb:90:insend' 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:276:inrun_callbacks' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/validations.rb:1110:in
valid_without_callbacks?' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/callbacks.rb:315:in `valid?'
我之前在同一个应用程序中的其他地方使用了相同的代码模式,并且该部分仍然正常工作(所有验证都按照它们应有的方式工作)。
有人可以阐明我做错了什么。
找出问题所在:
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 :: Validations提供的所有验证功能。 将关联重命名为更具体的事情可以解决问题。
正确实施课堂:
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是可选的。 我只是这么做的,所以我的命名约定在整个应用程序中都是一致的。
链接地址: http://www.djcxy.com/p/81119.html上一篇: RoR: ActiveRecord, NoMethodError in models that are extended from AR
下一篇: up gem in Rails 3