如何在记录中存储多个值

时间:2016-01-07 17:01:15

标签: ruby-on-rails angularjs

我有一个保存youtube电影链接的功能

angular.forEach(response.results, function(item){

  $scope.youtubeTrailer = [];
  $scope.youtubeTrailer.push(item.key);
  var youtubeLink = $scope.youtubeTrailer.toString();
  createTrailer.create({
    link:     youtubeLink,
    movie_id: $scope.movieListID.id
  }).then(init);

})

拖车导轨控制器,

def create
  respond_with Trailer.create(trailer_params)
end

private
def trailer_params
  params.require(:trailer).permit(
    :link,
    :movie_id
  )
end

拖车导轨模型,

belongs_to :movie

forEach为每个预告片创建一条新记录。

{"id":1,"movie_id":"312221","link":"LsjX5dqHLuw","created_at":"2016-01-05T14:15:35.681Z","updated_at":"2016-01-05T14:15:35.681Z"},
{"id":2,"movie_id":"281957","link":"EIELwayIIT4","created_at":"2016-01-05T14:46:25.248Z","updated_at":"2016-01-05T14:46:25.248Z"},
{"id":3,"movie_id":"291270","link":"WQkHA3fHk_0","created_at":"2016-01-05T14:50:15.563Z","updated_at":"2016-01-05T14:50:15.563Z"},
{"id":4,"movie_id":"291270","link":"LUloSK4x3qc","created_at":"2016-01-05T14:50:15.651Z","updated_at":"2016-01-05T14:50:15.651Z"},
{"id":5,"movie_id":"209112","link":"nIGtF3J5kn8","created_at":"2016-01-05T15:21:11.372Z","updated_at":"2016-01-05T15:21:11.372Z"},
{"id":6,"movie_id":"209112","link":"yViIi3gie2c","created_at":"2016-01-05T15:21:11.469Z","updated_at":"2016-01-05T15:21:11.469Z"},
{"id":7,"movie_id":"209112","link":"Onh7NbZ7F8o","created_at":"2016-01-05T15:21:11.550Z","updated_at":"2016-01-05T15:21:11.550Z"},
{"id":8,"movie_id":"312221","link":"LsjX5dqHLuw","created_at":"2016-01-05T15:26:19.701Z","updated_at":"2016-01-05T15:26:19.701Z"},

正如您可能会看到的那样,它为3个预告片创建了3条记录movie_id> 209112

是否可以保存数据,以便将具有相同movie_id的预告片组合在一起?所以它看起来像这样,

{"id":312221,
  "links"{
    {"link":"LsjX5dqHLuw","created_at":"2016-01-05T14:15:35.681Z","updated_at":"2016-01-05T14:15:35.681Z"},
  }
}

{"id":281957, 
  "links"{
    {"link":"EIELwayIIT4","created_at":"2016-01-05T14:46:25.248Z","updated_at":"2016-01-05T14:46:25.248Z"},
  }
}

{"id":291270",
  "links"{
    {"link":"WQkHA3fHk_0","created_at":"2016-01-05T15:21:11.550Z","updated_at":"2016-01-05T15:21:11.550Z"},
    {"link":"yViIi3gie2c","created_at":"2016-01-05T15:21:11.469Z","updated_at":"2016-01-05T15:21:11.469Z"},
    {"link":"LUloSK4x3qc","created_at":"2016-01-05T15:21:11.372Z","updated_at":"2016-01-05T15:21:11.372Z"},
  }
}

{"id":209112",
  "links"{
    {"link":"Onh7NbZ7F8o","created_at":"2016-01-05T15:21:11.550Z","updated_at":"2016-01-05T15:21:11.550Z"},
    {"link":"yViIi3gie2c","created_at":"2016-01-05T15:21:11.469Z","updated_at":"2016-01-05T15:21:11.469Z"},
    {"link":"nIGtF3J5kn8","created_at":"2016-01-05T15:21:11.372Z","updated_at":"2016-01-05T15:21:11.372Z"},
  }
}

{"id":312221,
  "links"{
    {"link":"LsjX5dqHLuw","created_at":"2016-01-05T15:26:19.701Z","updated_at":"2016-01-05T15:26:19.701Z"}
  }
}

1 个答案:

答案 0 :(得分:0)

您可能正确地在db中保存记录,您可能只需要在rails中创建嵌套的json响应。因此预告片包含在电影json中。

你可以用几种不同的方式做到这一点,但试试这个......

在您的控制器中,将respond_with更改为

def create
  @trailers = Trailer.create(trailer_params)
  respond_with Movie.where(:id => @trailers.map(:movie_id))
end

在他们的电影模型中,包括预告片集

class Movie < ActiveRecord::Base

  def as_json(options={})
    {
      :id          => id,
      :trailers    => trailers
    }
  end

end