Skip to content

Instantly share code, notes, and snippets.

@goosys
Created August 19, 2016 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goosys/aeb067db2423f1d6f39183dcb3a4bae2 to your computer and use it in GitHub Desktop.
Save goosys/aeb067db2423f1d6f39183dcb3a4bae2 to your computer and use it in GitHub Desktop.
[Rails]{ActiveRecord][PostgreSQL] sort columns
#
# config/initializers/extensions/active_record/attributes.rb
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/attributes.rb
#
module ActiveRecord #:nodoc:
module Attributes # :nodoc:
module ClassMethods
# Overriding method
def columns
# @columns ||= add_user_provided_columns(connection.schema_cache.columns(table_name)) #Original
@columns ||= add_user_provided_columns(connection.schema_cache.columns(table_name).sort!(&scheme_comparator))
end
private
# New method
def scheme_comparator
lambda do |a,b|
a.name.end_with?("_at") ?
(b.name.end_with?("_at") ? 0 : 1) :
(b.name.end_with?("_at") ? -1 : 0)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment