Tags again (now with Rails code)

I’ll put it here for my own reference:

def self.slow_has_all_tags(tags)
  p = Post.scoped
  pt = PostsTag.arel_table
  pt_arels = []
  tags.each do |t|
    t_id = Tag.where(:name => t).first[:id]
    pt_arels << pt.where(pt[:tag_id].eq(t_id)).project(pt[:post_id])
  end
  pt_arels.each do |q|
    p = p.where(:id => q)
  end
  p
end

And in fact, relatively fast.

Leave a Reply

Your email address will not be published. Required fields are marked *