Module: Ranktize

Included in:
ActiveRecord::Base
Defined in:
lib/ranktize.rb

Instance Method Summary collapse

Instance Method Details

#rank_by(*columns) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/ranktize.rb', line 2

def rank_by(*columns)
    # Loop through the arguments.
    columns.each do |column|
      # Define the method to get the ranking.
      # This is a workaround meant for mysql
      define_method "#{column}_ranking" do
        connection.select_value("SELECT COUNT(*) + 1 AS ranking FROM #{self.class.table_name} WHERE #{column} > (SELECT #{column} FROM #{self.class.table_name} WHERE id = #{self.id})").to_i
      end
    end
end