English 中文(简体)
Rails3: how to use relational algebra to replace SQL-constructions like NOT and OUTER JOIN
原标题:

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 )
最佳回答

Add to your Gemfile:

gem  meta_where 

and then:

Post.includes(:comment).where(:comments => {:tag.not_eq =>  my_tag })
问题回答

暂无回答




相关问题
(Lazy) LEFT OUTER JOIN using the Hibernate Criteria API

I want to perform a LEFT OUTER JOIN between two tables using the Criteria API. All I could find in the Hibernate documentation is this method: Criteria criteria = this.crudService ....

SQL: Get list of non-coupled items using MySQL 4

I m not that good in SQL and I ve come across a problem I don t know how to solve. I ve read and re-read parts of a book about SQL (O Reilly s Learning SQL) which I hoped would contain the information ...

How to do a full outer join in Linq?

I ve inherited a database that wasn t designed exactly optimally, and I need to manipulate some data. Let me give a more common analogy of the kind of thing I have to do: Let s say we have a Student ...

Problem with Full Outer Join not working as expected

I have a query to subtract current balance from one table with previous balance from another history table. When a particular product has no current balance but has previous balance, I am able to ...

Index usage with OUTER JOIN that contains IN statement

SELECT a.*, b.* FROM a LEFT OUTER JOIN b ON b.user IN (:userlist) AND b.key = a.fk_to_b WHERE a.user IN (:userlist) OR b.user IN (:userlist) Table b has an index of: (user, ...

can this be written with an outer join

The requirement is to copy rows from Table B into Table A. Only rows with an id that doesn t already exist, need to be copied over: INSERT INTO A(id, x, y) SELECT id, x, y FROM B b WHERE b.id IS NOT ...

Using outer joins in HQL queries

I m trying to build a query using HQL and OUTER JOINs and just can t get it work. Consider the following mapping <class name="Parent"> <id name="id" type="integer"> <...

热门标签