Require files on Seeds.Rb

Asked

Viewed 69 times

0

I need to organize my file Seeds.Rb, so I created a namespace where I will add my ex classes:

module Seeds
 class Apple
   def self.run
     puts 'teste'
   end
 end
end
Seeds::Apple.run

This works perfectly, but I need to separate my classes into separate files similar to the structure below:

db
  seeds
    Apple.rb
  seeds.rb

and in Seeds.Rb:

require 'db/seeds/Apple'
Seeds::Apple.run

and it returns an error that fails to load the file, which could be?

  • Do you know this Gem? https://github.com/james2m/seedbank Does not solve your problem?

  • Thank you, that will solve.

  • Okay, I’ll post as an answer.

2 answers

1

Use the code below to include all files in the folder Seeds

Utilise Rails.root to get the full path to file.

# Include ruby files
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
  puts load(filename) if File.exist?(filename)
end
Seeds::Apple.run
  • In addition to the Takitani answer working, she has a problem, Gems Gems Gems Gems, and Gems too much is not very cool not. Be sure before you use a Gem you really need, and with that code I bet it does what you ask.

0


  • 2

    While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed.

  • @Andersonbs Concordo!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.