Search code examples
ruby-on-railsrubyruby-on-rails-5activemodel

How to include a method in a Rails seed:db file?


I have the following Rail5 seeds.rb file:

s1-s4 are created above.

job_titles = JobTitle.all
job_titles.each_with_index do |job_title, index|
  case job_title.title
  when "XXXX"
    self.create_job_title_skills([s1,s2,s3,s4])
  end
end

def create_job_title_skills(items)    
  puts "create_job_title_skills"
  items.each do |skill|
    puts skill
  end    
end

I'm getting the following error:

NoMethodError: undefined method `create_job_title_skills' for main:Object

I only need this method for the seed file, how can I get a method to work in a Rails 5 seeds.rb file only?


Solution

  • Define the method before you call it, like this:

    def create_job_title_skills(items)    
      puts "create_job_title_skills"
      items.each do |skill|
        puts skill
      end    
    end
    
    job_titles = JobTitle.all
    job_titles.each_with_index do |job_title, index|
      case job_title.title
      when "XXXX"
        self.create_job_title_skills([s1,s2,s3,s4])
      end
    end