将多个对象存储到一个记录中

时间:2016-01-07 12:50:34

标签: ruby-on-rails angularjs

我有一个angular / rails应用程序,用户可以在其中搜索电影,然后将该电影添加到他们的监视列表中。

这是角度的创建函数,

$scope.movieCrew = response.credits.crew
$scope.movieCast = response.credits.cast

我在演员范围内有演员和工作人员数据,

def create
   @movie = Movie.find_or_create_by movie_params
   current_user.movies << @movie
   redirect_to :root
   current_user.followers.each { |follower| @movie.crete_activity(key: 'movie.create', owner: current_user, recipient: follower) }
end

private
def movie_params
  params.require(:movie).permit(
    :title,
    :image,
    :imdb_rating,
    :release_date,
    :movie_id,
    :backdrop
    :crew,
    :cast
  )
end

这些范围返回多个对象,因为涉及多个actor等。

我有一个movies_controller.rb,它有一个像这样的创建函数,

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   "backdrop"
  t.string   "crew"
  t.string   "cast"
end

我的电影模型看起来像这样,

{"id":26,"title":"The Neon Demon","release_date":"2016-01-21","image":"/gVB7zbrsk9TtLoVI4qob97q9v07.jpg","user_id":null,"created_at":"2016-01-07T12:31:49.956Z","updated_at":"2016-01-07T12:31:49.956Z","movie_id":"301365","backdrop":"/lSojrYBHLwiQQq6xr9Mec9f8wLM.jpg","crew":null,"cast":null}

当我运行动作时,会创建电影记录

Started POST "/movies.json" for 127.0.0.1 at 2016-01-07 13:31:49 +0100
Processing by MoviesController#create as JSON

  Parameters: {
    "release_date"=>"2016-01-21", 
    "imdb_rating"=>"N/A", 
    "title"=>"The Neon Demon", 
    "image"=>"/gVB7zbrsk9TtLoVI4qob97q9v07.jpg", 
    "movie_id"=>301365, 
    "backdrop"=>"/lSojrYBHLwiQQq6xr9Mec9f8wLM.jpg",

    "crew"=>[
      {"credit_id"=>"54881a95c3a368415c000cde", "department"=>"Directing", id"=>21183, job"=>"Director", name"=>"Nicolas Winding Refn", profile_path"=>"/tFqRY5LgXH6W9GV51xCiJS1y8mB.jpg"}, 
      {"credit_id"=>"54881a9c925141520a000c5b", department"=>"Writing", id"=>21183, job"=>"Screenplay", name"=>"Nicolas Winding Refn", profile_path"=>"/tFqRY5LgXH6W9GV51xCiJS1y8mB.jpg"}, 
      {"credit_id"=>"54881ac8c3a368414d000fd8", department"=>"Writing", ""=>1397141, job"=>"Screenplay", name"=>"Mary Laws", profile_path"=>nil}, 
      {"credit_id"=>"54881ad792514151ff000e94", department"=>"Camera", id"=>63422, job"=>"Director of Photography", name"=>"Philippe Le Sourd",         "profile_path"=>nil}, 
      {"credit_id"=>"54881ae5c3a3684148000cc8", department"=>"Sound", id"=>8377, job"=>"Original Music Composer", name"=>"Cliff Martinez", 
      "profile_path"=>nil}, 
      {"credit_id"=>"56424bf5c3a3686a5800064e", department"=>"Editing", id"=>966286, job"=>"Editor", name"=>"Matthew Newman", profile_path"=>nil}
      ], 

    "cast"=>[
      {"cast_id"=>5, "character"=>"Jesse", "credit_id"=>"54cd541dc3a3687f8f002102", "id"=>18050, "name"=>"Elle Fanning", "order"=>1, "profile_path"=>"/ouQIwOEVvyDzsU5j0Iw4oRIuGW4.jpg"}, 
      {"cast_id"=>6, "character"=>"Sarah", "credit_id"=>"54cd5425925141475700204d", "id"=>1036288, "name"=>"Abbey Lee", "order"=>2, "profile_path"=>"/tCLVClLA2dB43KdyqJusmcuvvEC.jpg"}, 
      {"cast_id"=>7, "character"=>"", "credit_id"=>"54d3a598c3a3686abf003f08", "id"=>20089, "name"=>"Jena Malone", "order"=>3, "profile_path"=>"/tx5KR6dAhYag3plX7Rdg8t25QrC.jpg"}, 
      {"cast_id"=>8, "character"=>"", "credit_id"=>"54d3a59c9251413fc1003e39", "id"=>6384, "name"=>"Keanu Reeves", "order"=>4, "profile_path"=>"/glCFGnKkX3QWxeLRYUMU1XTESHf.jpg"}, 
      {"cast_id"=>9, "character"=>"", "credit_id"=>"54d3a5a59251413fc1003e3b", "id"=>110014, "name"=>"Christina Hendricks", "order"=>5, "profile_path"=>"/4pAtKssXy84DzJOgl5155KJNS5z.jpg"}, 
      {"cast_id"=>10, "character"=>"", "credit_id"=>"54d3a5ad9251413fd6003e88", "id"=>234982, "name"=>"Bella Heathcote", "order"=>6, "profile_path"=>"/iqk9bZnrkLhIQbHPyrGcMK0Bzni.jpg"}
      ], 

    "movie"=>{
      "title"=>"The Neon Demon", 
      "release_date"=>"2016-01-21", 
      "image"=>"/gVB7zbrsk9TtLoVI4qob97q9v07.jpg", 
      "movie_id"=>301365, 
      "backdrop"=>"/lSojrYBHLwiQQq6xr9Mec9f8wLM.jpg", 

      "crew"=>[
        {"credit_id"=>"54881a95c3a368415c000cde", "department"=>"Directing", "id"=>21183, "job"=>"Director", "name"=>"Nicolas Winding Refn", "profile_path"=>"/tFqRY5LgXH6W9GV51xCiJS1y8mB.jpg"}, 
        {"credit_id"=>"54881a9c925141520a000c5b", "department"=>"Writing", "id"=>21183, "job"=>"Screenplay", "name"=>"Nicolas Winding Refn", "profile_path"=>"/tFqRY5LgXH6W9GV51xCiJS1y8mB.jpg"}, 
        {"credit_id"=>"54881ac8c3a368414d000fd8", "department"=>"Writing", "id"=>1397141, "job"=>"Screenplay", "name"=>"Mary Laws", "profile_path"=>nil}, {"credit_id"=>"54881ad792514151ff000e94", "department"=>"Camera", "id"=>63422, "job"=>"Director of Photography", "name"=>"Philippe Le Sourd", "profile_path"=>nil}, 
        {"credit_id"=>"54881ae5c3a3684148000cc8", "department"=>"Sound", "id"=>8377, "job"=>"Original Music Composer", "name"=>"Cliff Martinez", "profile_path"=>nil}, 
        {"credit_id"=>"56424bf5c3a3686a5800064e", "department"=>"Editing", "id"=>966286, "job"=>"Editor", "name"=>"Matthew Newman", "profile_path"=>nil}
      ], 

      "cast"=>[
        {"cast_id"=>5, "character"=>"Jesse", "credit_id"=>"54cd541dc3a3687f8f002102", "id"=>18050, "name"=>"Elle Fanning", "order"=>1, "profile_path"=>"/ouQIwOEVvyDzsU5j0Iw4oRIuGW4.jpg"}, 
        {"cast_id"=>6, "character"=>"Sarah", "credit_id"=>"54cd5425925141475700204d", "id"=>1036288, "name"=>"Abbey Lee", "order"=>2, "profile_path"=>"/tCLVClLA2dB43KdyqJusmcuvvEC.jpg"}, 
        {"cast_id"=>7, "character"=>"", "credit_id"=>"54d3a598c3a3686abf003f08", "id"=>20089, "name"=>"Jena Malone", "order"=>3, "profile_path"=>"/tx5KR6dAhYag3plX7Rdg8t25QrC.jpg"}, 
        {"cast_id"=>8, "character"=>"", "credit_id"=>"54d3a59c9251413fc1003e39", "id"=>6384, "name"=>"Keanu Reeves", "order"=>4, "profile_path"=>"/glCFGnKkX3QWxeLRYUMU1XTESHf.jpg"}, 
        {"cast_id"=>9, "character"=>"", "credit_id"=>"54d3a5a59251413fc1003e3b", "id"=>110014, "name"=>"Christina Hendricks", "order"=>5, "profile_path"=>"/4pAtKssXy84DzJOgl5155KJNS5z.jpg"}, 
        {"cast_id"=>10, "character"=>"", "credit_id"=>"54d3a5ad9251413fd6003e88", "id"=>234982, "name"=>"Bella Heathcote", "order"=>6, "profile_path"=>"/iqk9bZnrkLhIQbHPyrGcMK0Bzni.jpg"}
      ]
    }
  }

但是船员和演员的价值都是空的。

我已经检查了我的rails控制台,看看创建电影时会发生什么,

parameters

正如您所看到的,movie具有正确的值,即release_date,title,image,crew,cast等。但它不会将对象存储在其中。此外,我不确定为什么parameters值中的imdbRating值会重复{{1}}所需的所有数据。

所以我的问题是,如何存储工作人员的对象并投射到我的电影记录中?

0 个答案:

没有答案