【问题标题】:RoR: ActiveRecord, NoMethodError in models that are extended from ARRoR:从 AR 扩展的模型中的 ActiveRecord、NoMethodError
【发布时间】:2011-08-14 02:38:53
【问题描述】:

我设置了以下模型:

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

类 Qa::ErrorType # 关联 has_many :errors, :class_name => 'Qa::Error' has_many :evaluations, :class_name => 'Qa::Evaluation', :through => :errors
# 验证 validates_presence_of :内容 validates_uniqueness_of :内容 结尾

但是在保存/验证模型时,我不断遇到以下 NoMethodErrors:

<pre>NoMethodError (undefined method `add_on_blank' for #Class:0x23a3020):</pre>

例如:

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

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

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

【问题讨论】:

  • 解决了没关系,问题是关联 has_many :errors 覆盖了 ActiveRecord::Validations 提供的 'errors' 对象,解决方案是用更具体的名称重命名关联

标签: ruby-on-rails activerecord


【解决方案1】:

找出问题所在:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多