views:

315

answers:

1

There are two models Post and Comment. I should get all posts, which have no comments with specific tag. How can I do this using new Rails 3 features such relational algebra (arel).

SQL-solution should be something like this:

SELECT     `posts`.* FROM       `posts` LEFT OUTER JOIN `comments` ON `posts`.`id` = `comments`.`post_id`
WHERE    NOT (`comments`.`tag` = 'my_tag')
+2  A: 

Add to your Gemfile:

gem 'meta_where'

and then:

Post.includes(:comment).where(:comments => {:tag.not_eq => 'my_tag'})
hipertracker