积极记录关系有很多

时间:2012-10-25 07:00:23

标签: ruby-on-rails activerecord has-many-through

我有以下型号:

class FavoriteDirectorSet < ActiveRecord::Base
  has_many :links
  has_many :directors, through: :links
end

class Link < ActiveRecord::Base
  belongs_to :favorite_director_set
  belongs_to :director
end

class Director < ActiveRecord::Base
  has_many :links
  has_many :favorite_director_sets, through: :links
  has_many :movies
end

class Movie < ActiveRecord::Base
  belongs_to :director
end

我一直在通过将范围链接在一起来构建查询但我不清楚如何打破这个问题。如何创建与给定的FavoriteDirectorSet ID匹配的电影的Active Record Relation对象?

更新

我有两个解决方案(“favourite_director”缩写为fd):

1)@rubyman选项1:

fd_sets = FDSet.find(:fd_set_id)
res = Movie.where('director_id IN (?)', fd_sets.directors.map(&:id))

2)@rubyman选项2:

res = Movie.joins(:director=>[:links=>:fd_set]).
    where("fd_sets.id = ?", :fd_set_id)

2 个答案:

答案 0 :(得分:3)

单向可以说是fds = FavoriteDirectorSet.find(fds_id)

Movie.where('director_id IN(?)',fds.derectors.map(&amp;:id))

OR与联接(不确定)

Movie.joins(:director =&gt; [:links =&gt;:favorite_director_sets])。where(“favorite_director_sets.id =?”,fds_id)

答案 1 :(得分:0)

您可以根据RubyGuides

使用JOIN

我将使用的语法是:

@movie.joins("INNER JOIN favorite_director_sets ON favorite_director_sets.fk_director = movies.fk_director") # please ensure that table names are correct - if they follow convention, I believe they are correct

现在您可以添加条件,即favorite_director_sets.id = ?, fds_id

请注意,在您的情况下,您可能希望使用除LINNER之外的其他内容。在CoddingHorror上,可以很好地直观地解释INNER JOIN的含义以及替代方案。

更新:

请注意,其他解决方案要简单得多,但性能可能更差:

fds = FavoriteDirectorSet.find(fds_id)
@movies = fds.director.movies

或者你甚至可以用一行写出来:

@movies = FavoriteDirectorSet.find(fds_id).director.movies

更新: 这个答案是错误的,因为我忘记了Link表。 Rubyman的解决方案看起来很好