RoR:从 AR 扩展的模型中的 ActiveRecord、NoMethodError

发布于 2024-11-04 00:47:53 字数 3478 浏览 1 评论 0原文

我设置了以下模型: <代码>

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

类 Qa::ErrorType <质量保证::基础 set_table_name“错误类型”
# 协会 has_many :错误, :class_name => 'Qa::错误' has_many :评估, :class_name => 'Qa::评估', :through => :错误
# 验证 validates_presence_of :内容 validates_uniqueness_of :内容 结尾

但是在保存/验证模型时,我不断遇到以下 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?'

我之前在同一应用程序的其他地方使用过相同的代码模式,并且该部分仍然工作正常(所有验证都按预期工作)。

有人可以阐明我做错了什么吗?

I have the following models set up:

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

But when saving/validating a model I keep hitting the following NoMethodErrors:

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

Eg:

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

produces

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?'

I have used the same code pattern elsewhere in the same application before and that portion still works fine (all the validations work as they are supposed to).

Can someone shed some light on what I am doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2024-11-11 00:47:53

找出问题所在:

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 是可选的。我这样做是为了让我的命名约定在整个应用程序中保持一致。

Figured out what the problem was:

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

This declaration overrides the errors association/object that ActiveRecord provides hence we loose all validation functionality provided by ActiveRecord::Validations. Renaming the association to a more specific thing resolves the problem.

Correct implementation of the class:

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

All validations will work as they are meant to after this change. I think renaming the class Qa::Error as Qa::TransactionError is optional. I just did that so my naming convention is consistent throughout the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文