Skip to content

Instantly share code, notes, and snippets.

@jbodah
Created September 19, 2018 15:27
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 jbodah/1ab19a03e07befdb498c98fce5cf8dbc to your computer and use it in GitHub Desktop.
Save jbodah/1ab19a03e07befdb498c98fce5cf8dbc to your computer and use it in GitHub Desktop.
adding Arel-supported alias_column support to Rails
require 'arel/attributes/attribute'
module ColumnAlias
module ArelAttributePatch
def name
return super unless relation.engine.respond_to?(:column_aliases)
name_given = super
relation.engine.column_aliases[name_given] || name_given
end
end
module ActiveRecord
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def alias_column(from, to)
@column_aliases ||= {}
@column_aliases[from.to_s] = to.to_s
end
def column_aliases
@column_aliases
end
end
end
end
Arel::Attributes::Attribute.prepend(ColumnAlias::ArelAttributePatch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment