【问题标题】:Ruby & Mongoid - Uninitialized constantRuby & Mongoid - 未初始化的常量
【发布时间】:2016-05-29 08:14:15
【问题描述】:

我得到“未初始化的常量 SwimResult”:

class LegResult
  include Mongoid::Document
  field :secs, type: Float
  embedded_in :entrant
  embeds_one :event, as: :parent
  validates_presence_of :event

  after_initialize do |doc|
    doc.calc_ave
  end


  def calc_ave

  end

  def secs=value  
    self[:secs] = value
    calc_ave

  end
end


class SwimResult < LegResult
  field :pace_100,  as: :pace_100, type: Float

  def calc_ave
  end
end



class BikeResult < LegResult
  field :mph,  as: :mph, type: Float

  def calc_ave
  end
end

我知道当类位于单独的文件中时会发生这种情况,但在我的情况下,它们都在一个文件中 - 类 LegResult 和两个继承它的子类。如有任何帮助,我将不胜感激。

【问题讨论】:

  • 你的每个类都应该是它自己的文件,以类命名,例如。 app/models/swim_result.rb。这样 Rails 自动加载机制就可以正常工作了。

标签: ruby-on-rails ruby mongodb


【解决方案1】:

您需要将 SwimResult 和 BikeResult 类移动到swim_result.rb 和bike_result.rb 确保在使用前需要'leg_result',这将确保文件以正确的顺序加载

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多