Module: CoreExtensions::ActiveRecord::Sort::ClassMethods

Defined in:
lib/core_extensions/active_record/sort.rb

Instance Method Summary collapse

Instance Method Details

#sort(ids) ⇒ Object

A controller #sort action might for instance invoke this class method on an object. The array of ids are the model to sort, and sorting is done by updating each model’s position to reflect their place in the array.



24
25
26
27
28
# File 'lib/core_extensions/active_record/sort.rb', line 24

def sort(ids)
  Array(ids).each_with_index do |id, index|
    where(id: id).update_all(["position=?", index + 1])
  end
end