Full alias for table columns in ActiveRecord

I need something like alias_attribute

to create a complete alias for a column from the DB. For example, I have a table with many columns named UGLYCOLUMN

.

Using this in a query is very inconvenient:

MyModel.where('UglyColumn'.upcase => 'value')

      

I need something like:

class MyModel < ActiveRecord::Base
  awesome_alias_attribute {'UGLYCOLUMN' => :pretty_column, ...}
  ...
end

      

And then use MyModel.where(pretty_column: 'value')

instead MyModel.where('UglyColumn'.upcase => 'value')

.

Also, it should work for a complex query MyModel.joins(:other_relation).where(my_models: {pretty_column: 'value'})

, etc.

Ideally, it should work with all ActiveRecord methods and properties. That is, if I first create an alias for the primary key awesome_alias_attribute :UGLYPRIMARYKEY, :id

, then I want to say self.primary_key = :id

and profit.

+3


source to share





All Articles