astropanic astropanic - 3 months ago 38
Ruby Question

Rails model - NameError: uninitialized constant

I have two models:

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


app/models/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


In
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>


Why I get this error message ?

I know it happens when the rails environment is not loaded with the file of the class for which the method was called.

But why it happens in this particular case ?

Answer

Your filename has a s at the end field_of_studies_job_titles.