Ruby on rails RoR:ActiveRecord,从AR扩展的模型中的NoMethodError

Ruby on rails RoR:ActiveRecord,从AR扩展的模型中的NoMethodError,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我已设置了以下模型: 我以前在同一个应用程序的其他地方使用过相同的代码模式,但这一部分仍然可以正常工作(所有的验证都可以正常工作) 有人能解释一下我做错了什么。找出了问题所在: 类Qa::ErrorType'Qa::Error' 有很多:评估,:class\u name=>'Qa::Evaluation',:through=>:errors #验证 验证是否存在:内容 验证内容的唯一性 结束 此声明覆盖ActiveRecord提供的错误关联/对象,因此我们失去了ActiveRecord::

我已设置了以下模型:

我以前在同一个应用程序的其他地方使用过相同的代码模式,但这一部分仍然可以正常工作(所有的验证都可以正常工作)


有人能解释一下我做错了什么。

找出了问题所在:


类Qa::ErrorType
#联想
有很多错误,:class\u name=>'Qa::Error'
有很多:评估,:class\u name=>'Qa::Evaluation',:through=>:errors
#验证 验证是否存在:内容 验证内容的唯一性 结束

此声明覆盖ActiveRecord提供的错误关联/对象,因此我们失去了ActiveRecord::Validations提供的所有验证功能。将关联重命名为更具体的对象可以解决问题

类的正确实现:


类Qa::ErrorType
#联想
有很多:事务错误,:class\u name=>'Qa::TransactionError'
有很多:评估,:class\u name=>'Qa::Evaluation',:through=>:transaction\u错误
#验证 验证是否存在:内容 验证内容的唯一性 结束


所有的验证都将在这次更改后按预期工作。我认为将类Qa::Error重命名为Qa::TransactionError是可选的。我这样做是为了让我的命名约定在整个应用程序中保持一致。

解决了这个问题。没关系,问题是关联有很多:错误覆盖了ActiveRecord::Validations提供的“错误”对象,解决方案是用更具体的名称重命名关联 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 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: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:in
method_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:in
method_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:in
send'
    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: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:in
method_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:in
call' 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:in
call' 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:in
each' 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:in
run' 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: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 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