John Smith
John Smith

Reputation: 6269

Seed, run code from external file

I have a ruby file called company.rb in db\company.rb. This file should create instances of the class Company how you can see.

list = [["20", "ABC", ...
list.each do |c|
  Company.create(:Key => c[0] ...
end 

Now i try to make this file run in db\seeds.rb. I thought it would be executed if i requiere it in this file like this:

require 'company'

What do i wrong? Or how can i execute the code from company.rb in my seeds file? Thanks

Upvotes: 0

Views: 409

Answers (1)

James Mason
James Mason

Reputation: 4306

The db folder isn't going to be in your load path by default. Your require 'company' line is loading the wrong company.rb file (probably the one under app/models). The easiest solution is to use require_relative 'company' (assuming you're not still running Ruby 1.8). You could also:

  • Build the path manually with _FILE_ and #dirname.
  • Add db to your load path (don't do this, it's a bad idea)

Upvotes: 2

Related Questions