通过id连接两个模型

时间:2016-01-08 09:40:01

标签: ruby-on-rails

我有预告片模型和电影模型。创建电影时,我会查找所有可用的预告片,并将每个预告片放在一个记录中,其中包含youtube链接和已添加的电影的movie_id。

func greet(name: String, day: String)
greet("Anna", day: "Tuesday")

和json输出,

def index
  trailer = Trailer.all
  respond_to do |format|
    format.json do
      render :json => trailer.to_json(:only => [:id, :movie_id, :link])
    end
  end
end

然后我有我的电影控制器,

[  
  {  
    "id":1,
    "movie_id":"312221",
    "link":"LsjX5dqHLuw"
  },
  {  
    "id":2,
    "movie_id":"209112",
    "link":"nIGtF3J5kn8"
  },
  {  
    "id":3,
    "movie_id":"209112",
    "link":"yViIi3gie2c"
  },
  {  
    "id":4,
    "movie_id":"209112",
    "link":"Onh7NbZ7F8o"
  },
  {  
    "id":5,
    "movie_id":"290250",
    "link":"1Vb32Kokbtg"
  },
  {  
    "id":6,
    "movie_id":"290250",
    "link":"1Vb32Kokbtg"
  },
  {  
    "id":7,
    "movie_id":"27205",
    "link":"8hP9D6kZseM"
  },
  {  
    "id":8,
    "movie_id":"157336",
    "link":"ePbKGoIGAXY"
  },
  {  
    "id":9,
    "movie_id":"157336",
    "link":"KlyknsTJk0w"
  },
  {  
    "id":10,
    "movie_id":"157336",
    "link":"nyc6RJEEe0U"
  },
  {  
    "id":11,
    "movie_id":"157336",
    "link":"Lm8p5rlrSkY"
  },
  {  
    "id":12,
    "movie_id":"157336",
    "link":"zSWdZVtXT7E"
  }
]

json输出,

def index
  @movie = Movie.all
  respond_to do |format|
    format.json do
      render :json => @movie.to_json(include: :trailers)
    end
  end
end

有没有办法可以查找与电影具有相同movie_id值的所有预告片并将它们包含在json渲染中?

病态结果将是这样的,

[  
  {  
    "id":1,
    "title":"Creed",
    "release_date":"2016-01-21",
    "image":"/xSE4NBFDzqedwa4AIj99r1Z7ljF.jpg",
    "user_id":null,
    "created_at":"2016-01-07T20:19:43.849Z",
    "updated_at":"2016-01-07T20:19:43.849Z",
    "movie_id":"312221",
    "backdrop":"/nF4kmc4gDRQU4OJiJgk6sZtbJbl.jpg",
    "crew":null,
    "cast":null,
    "trailers":[  

    ]
  },
  {  
    "id":2,
    "title":"Batman v Superman: Dawn of Justice",
    "release_date":"2016-03-24",
    "image":"/eJrlh2g9UGAd7R6mQAOQIIs329H.jpg",
    "user_id":null,
    "created_at":"2016-01-07T20:21:02.615Z",
    "updated_at":"2016-01-07T20:21:02.615Z",
    "movie_id":"209112",
    "backdrop":"/15PbZtjRJ4zgQA8XS0otL70piQi.jpg",
    "crew":null,
    "cast":null,
    "trailers":[  

    ]
  },
  {  
    "id":3,
    "title":"The Nice Guys",
    "release_date":"2016-05-26",
    "image":"/ecD35nDfjsxvDW5BtmK6YAaIkzF.jpg",
    "user_id":null,
    "created_at":"2016-01-07T20:22:05.960Z",
    "updated_at":"2016-01-07T20:22:05.960Z",
    "movie_id":"290250",
    "backdrop":"/aEMBBMuK3BhKIuFu7iFSTXC41Bi.jpg",
    "crew":null,
    "cast":null,
    "trailers":[  

    ]
  },
  {  
    "id":4,
    "title":"Inception",
    "release_date":"2010-07-22",
    "image":"/qmDpIHrmpJINaRKAfWQfftjCdyi.jpg",
    "user_id":null,
    "created_at":"2016-01-08T09:22:30.383Z",
    "updated_at":"2016-01-08T09:22:30.383Z",
    "movie_id":"27205",
    "backdrop":"/s2bT29y0ngXxxu2IA8AOzzXTRhd.jpg",
    "crew":null,
    "cast":null,
    "trailers":[  

    ]
  },
  {  
    "id":5,
    "title":"Interstellar",
    "release_date":"2014-11-06",
    "image":"/nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg",
    "user_id":null,
    "created_at":"2016-01-08T09:22:39.120Z",
    "updated_at":"2016-01-08T09:22:39.120Z",
    "movie_id":"157336",
    "backdrop":"/xu9zaAevzQ5nnrsXN6JcahLnG4i.jpg",
    "crew":null,
    "cast":null,
    "trailers":[  

    ]
  }
]

2 个答案:

答案 0 :(得分:0)

您可以按movie_id

查询预告片
@movie = Movie.first
@trailers = Trailer.where(movie_id: @movie.id)

您可能想要研究协会。如果你的电影有很多预告片,那么你最终可以做到:

@trailers = @movie.trailers

Ruby on Rails Guide: ActiveRecord Associations

答案 1 :(得分:0)

您可以尝试在电影中显示预告片

你想在json中加入带电影的预告片

def index
 @movies = Movie.order('created_at desc')
   respond_to do |format|
     format.json do
       render :json => @movies.to_json(:include => { :trailers => { :only => [:id, :movie_id, :link] } })
     end
   end
 end

如果你想要包含电影的特定预告片

然后在电影模型中

对于rails> 4

has_many :trailers, -> { where(id: self.movie_id) }