Total ruby on rails newbie here.
I'm trying to populate a database within a ruby on rails framework. This is what my seeds.rb file looks like:
f = File.open("db/courses.xml")
doc = Nokigiri::XML(f)
f.close
doc.css("course").each do |node|
children = node.children
Course.create(:name => children.css("name"),
:description => children.css("description"))
end
I haven't been able to test this code, because the rake db:seed command keeps saying "No such file or directoy --- courses.xml". Note that I've both tried "courses.xml" and "db/courses.xml" as paths.
The file courses.xml is in the same folder as the seeds.rb file.
Try specifying the path from the root of the application. Like this:
f = File.open(File.join(Rails.root, 'db', 'courses.xml'))
Related
I want to populate a new feature with dummy data, but don't want to use the db/seeds.rb file as it already has seeds other data irrelevant for this feature.
To run the default seeds.rb file, you run the command rake db:seed.
If I create a file in the db directory called seeds_feature_x.rb, what would the rake command look like to run (only) that file?
Start by creating a separate directory to hold your custom seeds – this example uses db/seeds. Then, create a custom task by adding a rakefile to your lib/tasks directory:
# lib/tasks/custom_seed.rake
namespace :db do
namespace :seed do
Dir[Rails.root.join('db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb')
desc "Seed " + task_name + ", based on the file with the same name in `db/seeds/*.rb`"
task task_name.to_sym => :environment do
load(filename) if File.exist?(filename)
end
end
end
end
This rakefile accepts the name of a seed file in the db/seeds directory (excluding the .rb extension), then runs it as it would run seeds.rb. You can execute the rake task by issuing the following from the command line:
rake db:seed:file_name # Name of the file EXCLUDING the .rb extension
Update: Now it should also list the seed tasks when running rake --tasks or rake -T.
I tried out zeantsoi's answer but it didn't give me what I wanted, it did all files in a directory. Hacked away at it and got this.
namespace :db do
namespace :seed do
task :single => :environment do
filename = Dir[File.join(Rails.root, 'db', 'seeds', "#{ENV['SEED']}.seeds.rb")][0]
puts "Seeding #{filename}..."
load(filename) if File.exist?(filename)
end
end
end
And to use this do the following:
rake db:seed:single SEED=<seed_name_without_.seeds.rb>
This will look in the Rails.root/db/seeds folder for a file name without the .seeds.rb (it adds those for you).
Working example:
rake db:seed:single SEED=my_custom_seed
The above would seed the Rails.root/db/seeds/my_custom_seed.seeds.rb file
Too complicated!
I just wanted a simple task to execute every file under db/seeds directory without passing in any file names.
# lib/tasks/seed.rake
desc "Run all files in db/seeds directory"
namespace :db do
task seed: :environment do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
puts "seeding - #{filename}. for reals, yo!"
load(filename)
end
end
end
I'm trying to create some seed data and got this code from Railcasts. I've modified it slightly but doesn't seem to be working when i run bundle exec rake db:seed from the terminal. I get the following error in the terminal...
wrong number of arguments (0 for 1)
Below is my code in the seeds.rb file to populate the table. Is there a silly mistake in there somewhere?
require 'open-uri'
International.delete.all
open("international.txt") do |countries|
countries.read.each_line do |data|
code, country, currency = data.chomp.split("|")
International.create!(:code => code, :country => country, :currency => currency)
end
end
and my text file (stored in the same directory as the seeds.rb file is...
AU|Australia|AUD
CA|Canada|CAD
GB|United Kingdom|GBP
US|United States|USD
You need to pass an id to delete.
I assume you want to delete_all
International.delete_all
I have a file I'm trying to open in a Rails application. For some reason Ruby is splitting the name of the file.
For example:
root = Rails.root
path = root.join('lib/tasks/filename.shp')
puts path
What is output is /lib/tasks/filename/shp.
Then I run the command:
factory = Region::GEOFACTORY
RGeo::Shapefile::Reader.open(path, :factory => factory) do |file|
I get the error message:
Errno::ENOENT: No such file or directory - /lib/tasks/filename/.shp
It looks like the file has been split into filename and .shp?
Try
path = File.join(Rails.root, 'lib/tasks/filename.shp')
factory = Region::GEOFACTORY
RGeo::Shapefile::Reader.open(path, :factory => factory)
I have the following in my seeds.rb file. When I run rake db:seed, I get an error saying:
rake aborted!
No such file or directory - MSFT_1_100.json
This displays even though I have the file 'MSFT_1_100.json' in the same folder as the seeds.rb file (/project/db). Any advice on how to fix this?
require 'json'
file_name = 'MSFT_1_100.json'
data = File.open(file_name, "r").read
my_object = JSON.load(data)
my_object.each do |item|
new_review = Review.create(:company => 'Microsoft', :pro => item['pro'], :con => item['con'], :advice => item['advice'], :role => item['role'])
end
try to do this instead of just giving the file_name directly
file_name = File.expand_path(File.join(File.dirname(__FILE__),'MSFT_1_100.json'))
I need to create a recurring task that creates (or edits) product records from an external text file. From inside irb:
>> f = File.open(<filename>) # file in same directory path
No issues.
But when pasted into a Rake task file, the script always bombs "File not found". (Rails 3.1, Ubuntu.)
namespace :sap do
desc "uploads data from raw SAP file"
task :upload => :environment do
f = File.open("sap_pnlist_20111010a.csv")
records = f.readlines
records.each {|row|
... etc etc ...
}
end
end
Suggestions?
If the file is somewhere inside your Rails root, use
Rails.root.join('grandparent_dir', 'parent_dir', 'file.txt')
If the file is not in your Rails root, you must give it the full path.