Skip to content

Instantly share code, notes, and snippets.

@keeran
Created August 14, 2010 10:07
Show Gist options
  • Select an option

  • Save keeran/524179 to your computer and use it in GitHub Desktop.

Select an option

Save keeran/524179 to your computer and use it in GitHub Desktop.
scope :has_all_items_in_state, lambda {|item_states|
{
:joins => :order_items, :conditions => { :order_items => { :state => item_states}},
:select => "orders.*, COUNT(order_items.id) as item_count",
:group => "orders.id",
:having => "item_count = (select count(*) from order_items where order_id = orders.id)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment