1

I have a rails movie app and the movie info that will be featured on the site will be from TMDB.

Using the TMDB-Ruby gem, i can already produce results (info) from a movie, but now i need to know how to extract that returned info and save it to my database.

So if i run

TmdbMovie.find(:title => "fight club", :limit => 10, :expand_results => true, :language => "en")

in the rails console, it will show me info about that movie.

So How can I, from that returned info, create a new movie and save say the :title and the :description

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

1 Answers1

3

Looking at the documentation you can do it like this assuming you have a Movie class with the appropriate migrations for title and description.

movie = TmdbMovie.find(:title => "Iron Man", :limit => 1)
@movie.title = movie.title
@movie.description = movie.description # Not sure if the returned data contains a description 
@movie.save

EDIT~ In update to below comment.

Let's say you have your MovieController and a user is a searching for the movie through a search field which would be in params[:search] and the movie returned would be stored in the database. If this is being done in the index action it would look like below.

def index
  movie = TmdbMovie.find(:title => params[:search], :limit => 1)
  @movie = Movie.new
  @movie.title = movie.title
  @movie.description - movie.description
  @movie.save
end
Althaf Hameez
  • 1,511
  • 1
  • 10
  • 18
  • So the last three lines of your code would go in my controller under which action? and the first code would be in the CLI – PMP Jul 14 '13 at 16:24
  • you don't want anything of this to be in your Terminal. To choose which action it depends on what you want to be doing in your app. I'm assuming that the user is searching for the title. I'll update my answer to show an example. – Althaf Hameez Jul 14 '13 at 16:26
  • But if I want a movie, so say `Iron Man`, to be saved to my database, so it would display in a show action. It'll be like if I had created the movie myself using the `new.html.erb` page – PMP Jul 14 '13 at 16:30
  • Yes if you code your show action accordingly, it would be like you had created the movie yourself. As it seems you are a beginner have you taken a look at [Rails Tutorial by Michael Hartl](http://ruby.railstutorial.org/ruby-on-rails-tutorial-book?version=4.0), it covers a lot of Rails development and it's been updated for Rails 4.0 as well. It helped me a lot when I was learning rails, I probably have gone through it several times – Althaf Hameez Jul 14 '13 at 16:33
  • Yes i have followed that article when creating my first app. In my show action, say i have just `<%= @movie.title %>` and `<%= @movie.description %>`, if i run the code you provided, it'll create a new movie and add the tmdb title to `<%= @movie.title %>` ?? – PMP Jul 14 '13 at 16:38
  • Yes but you would also have to have the appropriate show controller logic as follows. def show @movie = Movie.find(params[:id]) end So if let's say the first movie that was saved was Iron Man, then if you go to movies/1/ it would show the Iron Man Title and Description. – Althaf Hameez Jul 14 '13 at 16:39
  • Yes I have that. So this is my index action `def index movie = TmdbMovie.find(:title => params[:search], :limit => 1) @movie = Movie.new @movie.title = movie.title @movie.description - movie.description @movie.save end` What do i do now for it to search for a movie? – PMP Jul 14 '13 at 16:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33446/discussion-between-althaf-hamez-and-pmp) – Althaf Hameez Jul 14 '13 at 16:43