Amit Pokhrel
Amit Pokhrel

Reputation: 290

Find records that are not associated with other model in HABTM

I have two models 'Article' and 'List' both associated with HABTM association. I also have 'articles_lists' join table that has 'article_id' and 'list_id'. Now I would like to display all the articles that are not associated with any lists. Which is articles which does not have entry in 'articles_lists' table.

How can I achieve this in Rails 5. Please help

Upvotes: 0

Views: 165

Answers (1)

widjajayd
widjajayd

Reputation: 6253

you can use

Article.includes(:articles_lists).where( :articles_lists => { :article_id => nil })

Upvotes: 2

Related Questions