I am using a rakefile to fetch information from one website and saving it to my database.
Using the TMDB-Gem, this code @movie = TmdbMovie.browse(:order_by => "release", :order => "asc", :page => 1, :per_page => 2, :language => "en", :expand_results => true)
browses the oldest movies (:order_by => "release"
) and saves them to my database, but as i'll be running this rake quite frequently, the returned and saved movies will be the same.
Every movie has a tmdb_id
and every id
is unique
How can i make the rakefile check that the returned movie's tmdb_id is unique, and if there is already a movie with that id, skip and save the next movie.
I tried it in my Movies model validates_uniqueness_of :tmdb_id
but it gives an error when running the rake command and it doesn't save movies.
Basically, how can I, through the rakefile, validate the uniqueness of the tmdb_id
This is my rake file
namespace :db do
task :pull_tmdb_data => :environment do
Tmdb.api_key = "API KEY"
Tmdb.default_language = "en"
@movie = TmdbMovie.browse(:order_by => "release", :order => "asc", :page => 1, :per_page => 2, :language => "en", :expand_results => true)
@movie.each do |movie|
Movie.create(title: movie.name, description: movie.overview, release_date: movie.released, tmdb_id: movie.id)
end
end
end