Just in time for the end of this apocolyptic year, I have completed my third project for Flatiron School. We were given additional time to work on this project, partly due to the holidays, and that was a great thing for me because I found myself running into multitudes of problems on this one.
My first tip here is this: Never underestimate how much you can use an API.
My first version of this project was making a database for my movies. I wanted to pull information from an API and be able to sort by actor, director, year... Using an API is a wonderful resource and I very much recommend it - if you are able to work within any restrictions they might have. Alas, I was not.
Next, I found a TSV file with all the information I needed! Awesome! Except the file was way too big to use and keep the information on GitHub.
So my final project, and solution to my problems, was a scaled back version of what I originally intended - a Lord of the Rings movie database. Honestly, it was really fun to build and allowed me to get a little more creative than I likely would have been with the original idea.
I still used a TSV file and used this to seed the databases. I found this an easy and useful way to get initial information into my project. It's really pretty easy for anyone who is able to find or create one.
I just created a TSV folder in db/seeds. I placed all the needed TSV files in this folder. This ended up looking like this:
Next, add the handy gem 'tsv' in your Gemfile, bundle install and then we need to write some code in our seed.rb file. This will be individualized for everyone and you can always refer to the TSV gem page for pointers.
Link
Since I created my own TSV file, my code here was pretty straightforward this time around. But the previous version of the project was a different story so I could see how this could get pretty complicated. Just keep in mind, the information likely is coming over in hashes and the first line is your key. This will help you figure out what you need. Also, use byebug or whatever debugger you prefer! That's what they are there for!
Here's what I ended up with:
require 'tsv'
TSV_ROOT = Rails.root.join('db', 'seeds', 'tsv')
file_name = "lotr-films.tsv"
file_path = File.join(TSV_ROOT, file_name)
TSV.parse_file(file_path).each do |line|
Movie.create(line.to_h)
end
Now, just run rails db:seed and we're all set!
This project was quite a challenge, but I feel like every challenge is just more opportunity to learn something else... so I must be learning tons! Check it out and let me know what you think!
Top comments (0)