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
sql ruby-on-rails activerecord


source to share


No one has answered this question yet

See similar questions:

49
Alias ​​column names in Rails

or similar:

2603
Add column with default value to existing table in SQL Server
1657
Finding duplicate values ​​in a SQL table
1562
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
1504
Inserting Stored Procedure Results into a Temporary Table
1377
The table name dilemma: singular versus multiple names
1339
How can I rename a database column in Ruby on Rails migrations?
1039
Get a list of all tables in Oracle?
996
Find all tables containing column with specified name - MS SQL Server
2
How to get values ​​from column named "object_id" in ActiveRecord?
0
Alias ​​for ActiveRecord columns in "where" function



All Articles
Loading...
X
Show
Funny
Dev
Pics