Skip to content

Instantly share code, notes, and snippets.

@oleglukashev
Created February 24, 2015 16:19
Show Gist options
  • Save oleglukashev/e42e1c85c4f5317b2442 to your computer and use it in GitHub Desktop.
Save oleglukashev/e42e1c85c4f5317b2442 to your computer and use it in GitHub Desktop.
module OrderedSelect
extend ActiveSupport::Concern
included do
def self.ordered_list(ids)
ids_list = ids.to_s.split(',').select(&:present?)
self.where(id: ids_list).order(ids_list.reverse.map { |id| "#{self.table_name}.id = #{id}" }.join(", "))
end
def self.unordered_list(ids)
ids_list = ids.to_s.split(',').select(&:present?)
self.where(id: ids_list)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment