Handle aliased attributes in AR::Relation - #7839
Conversation
There was a problem hiding this comment.
You should probably use the full ActiveSupport::HashWithIndifferentAccess constant here, since it's how it's used inside Rails.
|
@carlosantoniodasilva Fixed. Thanks for the feedback. |
|
We're doing some additional hash lookups, and HashWithIndifferentAccess is typically more expensive than a literal. Any performance degradation between this and the current master? You can get a cross section of different SQL query performance running |
|
There was a problem hiding this comment.
Please, use 1.9 hash syntax here.
|
@chancancode hey! still interested in keeping this up? |
|
Oops I thought I already pushed. Gimme a minute! |
|
In the first commit I only changed the code I touched, but that leaves the rest of the file with two different hash syntax styles, so I added a second commit to convert the rest of the file too. |
There was a problem hiding this comment.
That's not valid syntax.
|
@chancancode thanks for your changes, but I don't think that it's necessary to change the entire file right now. Also it makes harder to see the real diff of your pull request, can you please remove that? I know it's annoying to have the two hash syntaxes there, but we'll have to leave with it for a while. Thanks! |
|
It'd also need a changelog entry :) |
|
Done! Sent from my phone On 2012-11-28, at 5:55 PM, Carlos Antonio da Silva notifications@github.com wrote:
|
|
Already out of date. Damn CHANGELOGs... |
|
rebased On 2012-11-28, at 9:45 PM, Steve Klabnik wrote:
|
|
ping... what is the status on this one? |
|
Would this one also fix #6177 ? |
|
@senny once this gets in i'll take a look |
When using symbol keys, ActiveRecord will now translate aliased attribute names to the actual column name used in the database:
With the model
class Topic
alias_attribute :heading, :title
end
The call
Topic.where(heading: 'The First Topic')
should yield the same result as
Topic.where(title: 'The First Topic')
This also applies to ActiveRecord::Relation::Calculations calls such as `Model.sum(:aliased)` and `Model.pluck(:aliased)`.
This will not work with SQL fragment strings like `Model.sum('DISTINCT aliased')`.
Github rails#7839
*Godfrey Chan*
|
@tenderlove this should be good to go! Thanks for the help <333 ! |
…n_ar_relation Handle aliased attributes in AR::Relation
I started experimenting with
alias_attributein my STI models and got quite frustrated by the second-class support for attributes aliasing. Ideally if you aliased an attribute all the AR methods should be able to resolve the real column name automatically, just like if you have setself.table_name = ...then all of AR (associations, etc) will be made aware of that and do the right thing.This commit added support for aliased attributes in the finders, calculation methods, counting and pluck. This doesn't cover everything, but I believe it's a step in the right direction, and fixed this in places where it matters the most (finders).
@jonleighton and @rafaelfranca can you take a look? (This basically builds on top of #6800)