0

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

Amit Pokhrel
  • 282
  • 4
  • 21

1 Answers1

2

you can use

Article.includes(:articles_lists).where( :articles_lists => { :article_id => nil })
widjajayd
  • 6,090
  • 4
  • 30
  • 41
  • Thank you for the response but it is giving me error : ActionView::Template::Error (Can't join 'Article' to association named 'articles_lists'; perhaps you misspelled it?): – Amit Pokhrel Jul 30 '17 at 10:39
  • I just follow your information above, probably you can check your table name, probably article_lists ? just change my answer to follow your table name – widjajayd Jul 30 '17 at 14:30
  • Thank you it worked but with slight modification. I had to use singular for the join table in includes. i.e. Article.includes(:articles_list).where( :articles_lists => { :article_id => nil }) – Amit Pokhrel Aug 01 '17 at 06:05