Peter Boomsma
Peter Boomsma

Reputation: 9806

Store multiple values in database

I have two tables in my SQLite database. On table called movies and a table called trailers. The movie table has a few columns,

create_table "movies", force: :cascade do |t|
  t.string   "title"
  t.string   "release_date"
  t.string   "image"
  t.integer  "user_id"
  t.datetime "created_at",   null: false
  t.datetime "updated_at",   null: false
  t.string   "movie_id"
  t.string   "imdb_rating"
end

I want to add trailers to my movies. I've tried that by adding a column called trailers into my movies table, and then store multiple results in 1 column. But that didn't look like the right way to go.

So I've created the trailers table.

create_table "trailers", force: :cascade do |t|
  t.string   "movie_id"
  t.string   "link"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

My idea was to save every trailer link with the movie_id value so I maybe I could merge the two tables in 1 JSON file that I could use in my Angular app.

To retrieve the trailer data I've created a service,

service.trailer = function(youtube_link){
  return httpPromise(
    baseUrl + youtube_link + '/videos?api_key=a8f7039633f2065942***a28d7cadad4&callback=JSON_CALLBACK'
  )
};

The data returned from that service looks like this,

{"id":157336,"results":[
  {"id":"53db3c790e0a26189a000d09","iso_639_1":"en","key":"ePbKGoIGAXY","name":"Trailer 3","site":"YouTube","size":1080,"type":"Trailer"},
  {"id":"550df44b9251413554004d43","iso_639_1":"en","key":"KlyknsTJk0w","name":"Own it today","site":"YouTube","size":720,"type":"Trailer"},
  {"id":"533ec6fcc3a3685448009ccc","iso_639_1":"en","key":"nyc6RJEEe0U","name":"Teaser","site":"YouTube","size":720,"type":"Trailer"},
  {"id":"5376ab510e0a26141c0005a8","iso_639_1":"en","key":"zSWdZVtXT7E","name":"Trailer","site":"YouTube","size":720,"type":"Trailer"},
  {"id":"545da247c3a3685362005187","iso_639_1":"en","key":"Lm8p5rlrSkY","name":"Trailer 2","site":"YouTube","size":1080,"type":"Trailer"}
]}

So now I'm trying to save the data into the trailer table.

var mappedData = dataYoutube.results.map(function(r) {
  var obj = {}
  obj["movie_id"] = dataYoutube.id;
  obj["link"] = r.key
  return obj;
});
console.log(mappedData);

createTrailer.create({
  movie_id: mappedData.movie_id,
  link:     mappedData.key
})

And the createTrailer function in my service,

app.factory('createTrailer', ['$http', function($http){
  return{
    create: function(trailer){
      return $http.post('/trailers.json', trailer);
    }
  };
}])

On the Rails backend I've created the routes,

resources :trailers, only: [:create, :destroy, :index, :show]

A trailers_controller.rb

class TrailersController < ApplicationController
  def index
    respond_with Trailer.all
  end
  def create
  end
end

And a trailer_model.rb

class Trailer < ActiveRecord::Base
  belongs_to :movie
end

Currently when I do the save action I get an error in my rails console,

Started POST "/trailers.json" for 127.0.0.1 at 2015-12-04 16:04:25 +0100
Processing by TrailersController#create as JSON
  Parameters: {"movie_id"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}], "link"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}], "trailer"=>{"movie_id"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}], "link"=>[{"movie_id"=>210577, "link"=>"Ym3LB0lOJ0o"}]}}
  User Load (10.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 15ms (ActiveRecord: 17.1ms)

ActionView::MissingTemplate (Missing template trailers/create, application/create with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
  * "/home/alucardu/sites/movieseat/app/views"
  * "/home/alucardu/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/devise-3.5.2/app/views"
):

Upvotes: 0

Views: 148

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Based on your data above, you can make that an array of objects, each object having the movie_id and link column:

var mappedData = data.results.map(function(r) {
    var obj = {}
    obj["movie_id"] = data.id;
    obj["link"] = r.key

    return obj;
});

Fiddle: http://jsfiddle.net/muat8brp/

Upvotes: 1

Related Questions