ActiveRecord WHERE DOESN'T EXIST

Is there any way to use EXISTS with ActiveRecord besides find_by_sql?

I would like to find all the records without an association in a one-to-many relationship.

SELECT DISTINCT store_type FROM stores WHERE NOT EXISTS (SELECT * FROM cities_stores WHERE cities_stores.store_type = stores.store_type) 
+6
activerecord
source share
1 answer
 Store.all(:select => "DISTINCT store_type", :conditions => "NOT EXISTS (SELECT * FROM cities_stores WHERE cities_stores.store_type = stores.store_type)") 

ActiveRecord will execute the same request as you specified above. The returned storage instances will have one store_type attribute.

+5
source share

All Articles