2
这个问题最好通过一个例子来演示。我有2个Sequel
模型,Artists
和Genres
,通过一个木匠表通过许多一对多关系连接:续集模型过滤多对多关系
class Artist < Sequel::Model
many_to_many :genres,
left_key: :artist_id,
right_key: :genre_id,
join_table: :artists_genres
end
class Genres < Sequel::Model
many_to_many :artists,
left_key: :genre_id,
right_key: :artist_id,
join_table: :artists_genres
end
而且木匠表看上去很简单:
CREATE TABLE `artists_genres` (`artist_id` integer, `genre_id` integer)
我想什么做的是过滤所有属于特定流派的艺术家。使用单一类型时,这工作得很好:
some_genre = Genre[1]
Artists.dataset.where(:genres => some_genre).all
我的问题是试图让谁属于流派的集合所有的艺术家:
multiple_genres = Genre.where(:id => [1,2,4])
Artists.dataset.where(:genres => multiple_genres).all
这将返回谁属于任何3个流派的艺术家(1,2或4),而不是属于所有3种流派的艺术家。
什么是通过模型查询它的正确方法,或者重写为数据集join
调用来选择正确的行?
只是一个猜测:会不会'Artists.dataset.where(类型:1)。凡(类型2)。凡(流派:4)'工作? – mudasobwa