-
-
Save tonyblaze27/ea9e18b748ee57b0278df247e391c73c to your computer and use it in GitHub Desktop.
Revisions
-
arjunvenkat revised this gist
Nov 8, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ Make sure you've created a resource with the appropriate columns to match your s ## 2. Read in a CSV file Add the following lines to your `seeds.rb` file: require 'csv' -
arjunvenkat revised this gist
Jun 2, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ We'll keep building off this code until we've created a working seeds file. You require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'real_estate_transactions.csv')) csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') puts csv The new line converts the CSV file into a structure that Ruby can read. The `:headers => true` option tells the parser to ignore the first line of the CSV file. @@ -36,7 +36,7 @@ The new line converts the CSV file into a structure that Ruby can read. The `:he require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'real_estate_transactions.csv')) csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') csv.each do |row| puts row.to_hash end @@ -48,7 +48,7 @@ This new addition loops through the entire CSV file and converts each row of the require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'real_estate_transactions.csv')) csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') csv.each do |row| t = Transaction.new t.street = row['street'] -
arjunvenkat revised this gist
May 31, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,7 @@ The new line converts the CSV file into a structure that Ruby can read. The `:he puts row.to_hash end This new addition loops through the entire CSV file and converts each row of the document into a hash. The headers of the CSV file will be used as keys for the hash because we added the `:headers => true` option in our previous step. ## 5. Create a database object from each row -
arjunvenkat revised this gist
May 31, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,7 @@ The new line converts the CSV file into a structure that Ruby can read. The `:he puts row.to_hash end This new addition loops through the entire CSV file and converts each row of the document into a hash. The headers of the CSV file will be used as keys for the hash. ## 5. Create a database object from each row -
arjunvenkat revised this gist
May 31, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,7 @@ The new line converts the CSV file into a structure that Ruby can read. The `:he puts row.to_hash end This new addition loops through the entire CSV file and converts each row of the document into a hash. The headers of the CSV file will be used as keys for hash. ## 5. Create a database object from each row -
arjunvenkat revised this gist
May 29, 2015 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,7 +59,7 @@ This new addition loops through the entire CSV file and converts each row of the t.beds = row['beds'] t.sq_feet = row['sq_feet'] t.category = row['type'] t.sale_date = row['sale_date'] t.price = row['price'] t.lat = row['latitude'] t.lng = row['longitude'] @@ -69,4 +69,3 @@ This new addition loops through the entire CSV file and converts each row of the puts "There are now #{Transaction.count} rows in the transactions table" -
arjunvenkat revised this gist
May 29, 2015 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ Add the following lines to your `seed.rb` file: require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'real_estate_transactions.csv')) puts csv_text The first line requires the Ruby CSV library we need to properly parse the CSV data. The next line reads in the CSV file into a variable. The last line prints the contents of the variable. When you run `rake db:seed` you should see a wall of text representing your CSV data. It's a first step, but we've still got a lot of work to do. @@ -25,7 +25,7 @@ We'll keep building off this code until we've created a working seeds file. You require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'real_estate_transactions.csv')) csv = CSV.parse(csv_text, :headers => true) puts csv @@ -35,7 +35,7 @@ The new line converts the CSV file into a structure that Ruby can read. The `:he require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'real_estate_transactions.csv')) csv = CSV.parse(csv_text, :headers => true) csv.each do |row| puts row.to_hash @@ -47,7 +47,7 @@ This new addition loops through the entire CSV file and converts each row of the require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'real_estate_transactions.csv')) csv = CSV.parse(csv_text, :headers => true) csv.each do |row| t = Transaction.new -
arjunvenkat revised this gist
May 29, 2015 . 1 changed file with 9 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +1,27 @@ # How to seed a Rails database with a CSV file ## 1. Setup First, Create a folder inside of `lib` called `seeds` Put your CSV file `example.csv` into the `lib/seeds` folder. In the example below, the file is called `real_estate_transactions.csv` Make sure you've created a resource with the appropriate columns to match your seed data. The names don't have to match up. ## 2. Read in a CSV file Add the following lines to your `seed.rb` file: require 'csv' csv_text = File.read('lib/seeds/real_estate_transaction.csv') puts csv_text The first line requires the Ruby CSV library we need to properly parse the CSV data. The next line reads in the CSV file into a variable. The last line prints the contents of the variable. When you run `rake db:seed` you should see a wall of text representing your CSV data. It's a first step, but we've still got a lot of work to do. We'll keep building off this code until we've created a working seeds file. You should be able to run `rake db:seed` at the end of each step ## 3. Parse the CSV require 'csv' @@ -29,7 +31,7 @@ We'll keep building off this code until we've created a working seeds file. You The new line converts the CSV file into a structure that Ruby can read. The `:headers => true` option tells the parser to ignore the first line of the CSV file. ## 4. Looping through the parsed data require 'csv' @@ -41,7 +43,7 @@ The new line converts the CSV file into a structure that Ruby can read. The `:he This new addition loops through the entire CSV file and converts each row of the document into a hash. THe headers of the CSV file will be used as keys for hash. ## 5. Create a database object from each row require 'csv' -
arjunvenkat revised this gist
May 29, 2015 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ This new addition loops through the entire CSV file and converts each row of the ## Create a database object from each row require 'csv' csv_text = File.read('lib/seeds/real_estate_transactions.csv') csv = CSV.parse(csv_text, :headers => true) csv.each do |row| @@ -57,14 +57,14 @@ This new addition loops through the entire CSV file and converts each row of the t.beds = row['beds'] t.sq_feet = row['sq_feet'] t.category = row['type'] t.sale_date = DateTime.parse(row['sale_date']) t.price = row['price'] t.lat = row['latitude'] t.lng = row['longitude'] t.save puts "#{t.street}, #{t.city} saved" end puts "There are now #{Transaction.count} rows in the transactions table" -
arjunvenkat revised this gist
May 29, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -62,6 +62,7 @@ This new addition loops through the entire CSV file and converts each row of the t.lat = row['lat'] t.lng = row['lng'] t.save puts "#{t.street}, #{t.city} saved" end puts "There are now #{Transaction.count} rows in the transactions table" -
arjunvenkat renamed this gist
May 29, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
arjunvenkat created this gist
May 29, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ # How to seed a Rails database with a CSV file Create a folder inside of `lib` called `seeds` Put your CSV file `example.csv` into the `lib/seeds` folder Make sure you've created a resource with the appropriate columns to match your seed data. The names don't have to match up. ## 1. Read in a CSV file Add the following lines to your `seed.rb` file: require 'csv' csv_text = File.read('lib/seeds/example.csv') puts csv_text The first line requires the Ruby CSV library we need to properly parse the CSV data. The next line reads in the CSV file into a variable. The last line prints the contents of the variable. When you run `rake db:seed` you should see a wall of text representing your CSV data. It's a first step, but we've still got a lot of work to do. We'll keep building off this code until we've created a working seeds file. You should be able to run `rake db:seed` at the end of each step ## 2. Parse the CSV require 'csv' csv_text = File.read('lib/seeds/real_estate_transactions.csv') csv = CSV.parse(csv_text, :headers => true) puts csv The new line converts the CSV file into a structure that Ruby can read. The `:headers => true` option tells the parser to ignore the first line of the CSV file. ## 3. Looping through the parsed data require 'csv' csv_text = File.read('lib/seeds/real_estate_transactions.csv') csv = CSV.parse(csv_text, :headers => true) csv.each do |row| puts row.to_hash end This new addition loops through the entire CSV file and converts each row of the document into a hash. THe headers of the CSV file will be used as keys for hash. ## Create a database object from each row require 'csv' csv_text = File.read('lib/seeds/real_estate_transactions.csv') csv = CSV.parse(csv_text, :headers => true) csv.each do |row| t = Transaction.new t.street = row['street'] t.city = row['city'] t.zip = row['zip'] t.zip = row['zip'] t.state = row['state'] t.beds = row['beds'] t.sq_feet = row['sq_feet'] t.category = row['type'] t.sale_date = row['sale_date'] t.price = row['price'] t.lat = row['lat'] t.lng = row['lng'] t.save end puts "There are now #{Transaction.count} rows in the transactions table"