Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/relation_ext.rb

Instance Method Summary collapse

Instance Method Details

#percentage(*filters) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/active_record/relation_ext.rb', line 5

def percentage(*filters)
  return 0 if count == 0

  if filters.first.is_a?(Symbol)
    filtered = filters.inject(self) do |relation, scope|
      relation.public_send(scope)
    end
  else
    filtered = where(*filters)
  end

  filtered.count * 1.0 / count
end