Named Scope Example with Lambda and Group By Having

Ruby code posted by Tony Perrie
created at 24 Oct 01:45

Edit | Back
1
2
3
4
5
  named_scope :linked_to, 
  lambda { |ovs| { :select => "domain_objects.*, count(links.from_id) AS object_count", 
                   :conditions => [ "links.object_value_id IN (?)", ovs ], 
                   :joins => "INNER JOIN links ON links.from_id = domain_objects.id", 
                   :group => "links.from_id HAVING object_count >= #{ovs.size}" } }
368 Bytes in 4 ms with coderay