How to import to rake file to table? - ruby-on-rails

hi i m new to ROR i need to how to import following data to table City in Database
This file is in libs>>tasks>>
desc "Import City to ActiveRecord table"
task :city_import, [:argument] => :environment do
puts "Importing City ***"
City.create(:name=>'Balaghat',:state_id=>State.find_by_name('Madhya Pradesh'));
City.create(:name=>'Belgaum',:state_id=>State.find_by_name('Karnataka'));
end
i try to using rake in terminal but not working

In terminal go to your app and type
rake city_import

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 3.1.0: Run Insert Script while doing rake db:migrate

I have created some table and I want to insert some data into the table. How shall I place my code when I commit the code and some of my peers pull the code and run rake db:migrate then on their db tables should be created along with the data?
You should use seeds
As it says in db/seeds.rb add in ruby code and then run rake db:seed
5.times do |i|
Product.create(name: "Product ##{i}", description: "A product.")
end

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.

Ruby Script to create new model value

Project is the model name and I want to do something like:
Project.create(:name => 'projectname', :identifier => 'projectidentifier')
This should be done in the terminal through a ruby script. I am not going to use rails console to create it nor use seeds.rb in a db file to migrate this as rake db:seed.
Can someone help. Thanks
The easiest way would be using rails runner (which essentially loads rails):
rails runner your_script.rb
The line of code would be a content of that script.
What about a rake task?
on lib/tasks, create a file named data.rake, and the content:
namespace :data
desc "Create project data"
task create_project_data: :environment do
Project.create(name: 'projectname', identifier: 'projectidentifier')
end
end
And you can run it as any rake task
rake data:create_project_data
And it will also appear when you list your rake tasks
rake -T

Resources