Activerecord is equivalent to SQL 'minus'

What rails a way to subtract a query result from another? Sample SQL for the database:

SELECT Date FROM Store_Information MINUS SELECT Date FROM Internet_Sales 
+4
source share
1 answer

I will throw this into the mix - not a solution, but it can help in progress:

Best of all, I can think of using NOT IN:

 StoreInformation.where('date NOT IN (?)', InternetSale.all) 

What Rails 3 - Rails 2 will be:

 StoreInformation.all(:conditions => ['date NOT IN(?)', InternetSale.all]) 

But both of them will first select everything from internet_sales; what you really want is a subquery to do all this in the database engine. For this, I think you will have to break find_by_sql and just give a subquery.

Obviously, this assumes you are using MySQL! NTN.

+4
source

All Articles