In this article we will be going over how to seed your data with a fetched api in ruby.
In this example, we will be using the tmdb api. In your seeds.rb file, you will first need to require some gems at the top of the file.
Second, you will post the api you will be utilizing and set it to a variable. Don’t forget to destroy your seeded data every time you re-seed.
From here we will create a method that will allow us to parse this api as json.
Now we will pass in the popular_movies_api as a parameter to a new GetRequester and assign that to a variable. Then we will take that variable and get the response body.
Let’s take a look at the api we are grabbing data from.
This database is what our variable popularMovies now looks like. We now have to set our values to what we want to grab from the database.
As you can see above, every movie is nested in the “results.” We are grabbing each movie and setting variables for the values that we want to grab.
Now your seeded data should look like this.
Thanks for reading!