Question

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
Was it helpful?

Solution

You're right to use validates_uniqueness_of :tmdb_id in your Movie model. You didn't show how you were saving the movies to your database, but .save doesn't cause an exception when validations fail, whereas .save! does. The key would be to use a save method that doesn't raise an error when validations fail.

Edit - Now that I understand what you're actually trying to do, you should be able to do something like:

per_page = 100
number_of_movies = Movie.count
page = number_of_movies/per_page+1
@movies = TmdbMovie.browse(:order_by => "release", :order => "asc", :page => page, :per_page => per_page, :language => "en", :expand_results => true) browses the oldest movies (:order_by => "release")

So if you've already pulled 435 movies, it'll only return movies 400-500 in the next call .. I did it this way because I wasn't sure if there was an offset option, but if there is you could just offset the query by Movie.count, which would be better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top