0

I am using the ruby tmdb gem to find movies from TMDB. I have my API key set up and I know how to find a movie in the rails console

So Say I run this @movies = TmdbMovie.find(:title => 'The Social Network') in the rails console This returns a whole bunch of information about the aforementioned movie, The Social Network.

What I need to know, is how I can save the returned movie information to my database, and potentially create a new movie based on the returned information.

So say it run @movies = TmdbMovie.find(:title => 'The Social Network') and the console returns this View Gist on Github

What if i want to save, from that returned info, just the description, trailer and title to my database

Travis Bell
  • 204
  • 2
  • 7
PMP
  • 231
  • 6
  • 25

2 Answers2

0

Assuming you have a model set up to contain this data, you could do:

@movies.each do |movie|
  # assumes @movies is an array
  # inside this loop, "movie" refers to the current TMDB movie object
  YourMovie.where({
    description: movie.description,
    trailer: movie.trailer,
    title: movie.title
  }).first_or_create
end

YourMovie is just a reference to whatever you want to call your movie model; in your case (with a DB table called movies) it would be Movie.

If you don't already have an ActiveRecord model set up, you can generate one easily: rails g model YourMovie description trailer movie.

If you don't really need to store the movie in your local database, and just want to prevent hitting the API service constantly, you could use Rails low-level caching:

Rails.cache.fetch('The Social Network', expires_in: 24.hours) do
  @movies = TmdbMovie.find(:title => 'The Social Network')
end
Jacob Brown
  • 7,221
  • 4
  • 30
  • 50
  • If i've already got a Movies table, with a model? What is YourMovie? because there is `movies` and `yourmovie` in your code. Where would the first piece of code go? Where would the last piece of code go? And what if I need to add and save multiple movies with different titles – PMP Jul 05 '13 at 18:10
  • I updated my answer to respond to your first couple questions. Where you run this code depends on what you are trying to do? Do you want to load items into the database yourself, or do you just want them loaded when a user searches for a title (i.e., in a controller). You can run both pieces of code from the Rails `console` if you like. – Jacob Brown Jul 05 '13 at 18:31
  • I would prefer to have it saved in my database – PMP Jul 05 '13 at 18:34
  • So which code would I use and how would i save it to my database? – PMP Jul 06 '13 at 00:15
  • The first code block; if you have it set up correctly that block will save all movies in `@movies` to your database. – Jacob Brown Jul 06 '13 at 00:42
  • So would this code go in the show.html.erb page for the movies? What I'm asking is what code would I run to fetch a movie from TMDB and save it to my database. – PMP Jul 06 '13 at 10:43
  • Probably not `show.html.erb`. Where are you loading `@movies`? Is the title search in your example going to be created from a user form? – Jacob Brown Jul 06 '13 at 13:23
  • I want to run a query, fetch a movie from TMDB and save it to my database, that movie will be the same as if I had created it myself on the `new.html.erb` page, it will have a destroy, edit and show action – PMP Jul 06 '13 at 17:37
  • I'm trying to achieve the same thing as PMP tried to do. But I'm having a bit of a hard time understanding your code. Could you make a example of a search field you would use to fit the create code? – Peter Boomsma Oct 03 '14 at 13:57
0

I am not sure if you are legally allowed to save the tmdb data into your own database.You need to hit their api to get the movie info or cache it from time to time. You should not save it into your database.... The solution provided by kardeiz is the way to go....

rails4sandeep
  • 1,797
  • 2
  • 14
  • 16