Rails模型 - NameError:未初始化的常量

时间:2016-09-06 13:57:05

标签: ruby-on-rails ruby

我有两种模式:

app/models/data_science/field_of_studies_job_titles.rb

module DataScience
  class FieldOfStudiesJobTitle < ActiveRecord::Base
    self.table_name = 'ds_campus_transitions_disciplines_current'
    self.primary_keys = :fieldofstudy, :jobrole
  end
end

应用程序/模型/ field_of_studies_job_title.rb

class FieldOfStudiesJobTitle < ActiveRecord::Base
  belongs_to :job_title
  belongs_to :field_of_study

  validates :job_title_id, uniqueness: { scope: :field_of_study_id }

  def self.import(source)
    source.find_in_batches do |batch|
    batch.each do |item|
      puts "huhu"
    end
  end
end

rails console

bundle exec rails c
Loading development environment (Rails 4.2.7)
irb(main):001:0> FieldOfStudiesJobTitle.import(::DataScience::FieldOfStudiesJobTitle)
NameError: uninitialized constant DataScience::FieldOfStudiesJobTitle
    from (irb):1
    from /usr/local/bundle/gems/railties-4.2.7/lib/rails/commands/console.rb:110:in `start'
    from /usr/local/bundle/gems/railties-4.2.7/lib/rails/commands/console.rb:9:in `start'
    from /usr/local/bundle/gems/railties-4.2.7/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /usr/local/bundle/gems/railties-4.2.7/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /usr/local/bundle/gems/railties-4.2.7/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
irb(main):002:0>

为什么我收到此错误消息?

我知道当rails环境没有加载调用该方法的类的文件时会发生这种情况。

但为什么会在这种特殊情况下发生?

1 个答案:

答案 0 :(得分:3)

您的文件名末尾有s {/ 1}}。