2017-03-09 106 views
-1

我有2個表:PostCategory有許多一對多的關係(categories_posts是連接表)過濾器被許多一對多的關係

我有一個類別ID列表[2,5,7]。我怎樣才能找到連接到所有這些類別的帖子?

例如,如果我有2類:foodenglish,我需要找到的所有帖子,大約foodenglish語言。

我試過這個,但它不起作用。 (filters是類IDS)

scope :filter_by_categories, -> filters { 
    where(categories_posts_ids.contains filters) 
} 

has_many :categories_posts 
has_many :categories, through: :categories_posts 

回答

-1

在Rails 5,你可以這樣做: Post.left_joins(:類)。凡( '?categories_id IN',IDS)