ruby-on-railsrubytagsfindacts-as-taggable-on

Rails, acts_as_taggable_on: how to get all aricles without tags


I'm using acts-as-taggable-on. I have Article model:

class Article < ActiveRecord::Base
  acts_as_taggable_on :tags
end

I know how to find all articles with tag "tag". According to README the solution is: Article.tagged_with("tag").

But how to find all Articles without any tags?


Solution

  • Use a classic SQL trick: left join then select lines where second ID is null.

    Article.
      joins(%Q{LEFT JOIN taggings ON taggings.taggable_id=articles.id AND taggings.taggable_type='Article'}).
      where('taggings.id IS NULL')