Rails Seed Data from file - ruby-on-rails

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

Related

The case of the disappearing ActiveRecord attribute

Following the instructions in https://stackoverflow.com/a/24496452/102675 I wound up with the following:
namespace :db do
desc 'Drop, create, migrate, seed and populate sample data'
task seed_sample_data: [:drop, :create, :migrate, :seed, :populate_sample_data] do
puts 'Sample Data Populated. Ready to go!'
end
desc 'Populate the database with sample data'
task populate_sample_data: :environment do
puts Inspector.column_names.include?('how_to_fix')
# create my sample data
end
end
As you would expect, I get true if I run bundle exec rake db:populate_sample_data
BUT if I run bundle exec rake db:seed_sample_data I get all the migration output and then false. In other words I can't see the Inspector attribute how_to_fix even though it definitely exists as proved by the other rake run. Where did my attribute go?
My guess is that this is a "caching" problem. Can you try the following?
task populate_sample_data: :environment do
Inspector.reset_column_information
# ...
end
P.S. We used to have a similar problem working with different databases having the exact same schema (only except some columns here and there)

Csv import to database form url

I'm trying to have this url place everything in the csv into my database.
here is the code i have so far
require 'open-uri'
namespace populate do
task reload: :environment do
Afftable.delete_all
url = 'something.com/format=csv'
CSV.open(url).each do |row|
Afftable.create(name: row[0], address: row[1])
end
end
end
When i try running this command i get this error
Command: bundle exec rake populate:reload
Error: NameError: undefined local variable or methodpopulate' for main:Object`
My database has all the headers in the csv file already inside the database. (i've not touched the create yet as i don't really know what im doing with this)
I think that you need a colon in front of populate.
namespace :populate do

Rails convention for table with just one row

Is there any conventions of rails or a right way to create/manipulate a table that will contain just one row?
If not, what is the best way to do that?
I need a way to store configurations of the system.
Thanks.
Edited:
The rake db:seed command, basically execute whatever code you write in db/seeds.rb file of your application. Though can write any code in this file, by convention you should write code which populate your database with the basic data,
for example: when ever your deploy your application somewhere, and create a new database for it, you want that user with admin credential must be present there. So you will write the code which create that user in this file. Below is the sample code which will create a user and assign admin role to him.
puts "********Seeding Data Start************"
admin = User.create(:first_name => 'System', :last_name => 'Admin',
:email => 'systemadmin#sunpower.com', :password => 'sunpoweradmin',
:password_confirmation => 'sunpoweradmin', :source_system_id => 'systemadmin',
:source_system => 'LP',:entity_type => "Customer", :target_system => "OPENAM")
if admin.errors.blank?
puts "***User #{admin.first_name} #{admin.last_name} created ***"
admin.add_role :admin # add_role is method defined by rolify gem
puts "***admin role assigned to #{admin.first_name} #{admin.last_name}***"
else
puts "admin user failed to create due to below reasons:"
admin.errors.each do |x, y|
puts"#{x} #{y}" # x will be the field name and y will be the error on it
end
end
puts "********Seeding Data End************"
Now whenever you recreate your database, you just need to run below command to populate the database, with the basic data
$ rake db:seed RAILS_ENV=production
The correct order to setup database in production, with all the rake task available within db namespace is as below
$rake db:create RAILS_ENV=production
$rake db:migrate RAILS_ENV=production
$ rake db:seed RAILS_ENV=production
NOTE: You can replace the first two commands with $rake db:setup RAILS_ENV=production , it will run both create and migrate internally
OR
You could use the rails-settings-cached gem which is a fork of the rails-settings gem
Once setup, you'll be able to do things such as:
Setting.foo = 123
Setting.foo # returns 123
Hope this may help you or what you are looking for..

Rake db:seed can't find .xml file

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'))

Import data from text file into PostgreSQL. "Postgres_Copy" Gem

I am using PostgreSQL with ruby and rails app and have Postgres_copy gem installed
https://github.com/diogob/postgres-copy
I have hotels table and want to import text file using function pg_copy_from the gem. Data in the file is separated by the "|" symbol.
So I have created rake task
namespace :db do
namespace :import do
desc "Copy hotels records to the database"
task :hotels => :environment do
Hotel.pg_copy_from 'db/ActivePropertyList.txt', :delimiter => '|', :map => {
'HotelID' => 'ean_hotel_id',
'SequenceNumber' => 'sequence_number',
'Name' => 'name',
'Address' => 'address'}
end
end
end
If i run rake db:import:hotels nothing happens. What could be the problem?
UPDATE
I noticed that when I run psql and try to see all realtions by \d command, it says "No relations found."
However, I ran rake:db:create:all and rake db:migrate commands and can see the schema file. Also, \l command in psql console shows my database.
Maybe it would be easier just to run
c = Hotel.connection.raw_connection
c.exec(%q{\copy hotels (hotel_id, name) FROM 'db/ActivePropertyList.txt' DELIMITER '|'})
, but I suggest nothing happens because of the missing relations.
My import failed because of the upper-case issue that is common for the PostgreSQL. Renaming all header fields and database column names to lower case did the trick.

Resources