-
Notifications
You must be signed in to change notification settings - Fork 21.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle aliased attributes in AR::Relation #7839
Handle aliased attributes in AR::Relation #7839
Conversation
@@ -66,7 +67,7 @@ module AttributeMethods | |||
|
|||
included do | |||
class_attribute :attribute_aliases, :attribute_method_matchers, instance_writer: false | |||
self.attribute_aliases = {} | |||
self.attribute_aliases = HashWithIndifferentAccess.new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
|
@@ -28,7 +28,8 @@ def test_find_with_string | |||
def test_exists | |||
assert Topic.exists?(1) | |||
assert Topic.exists?("1") | |||
assert Topic.exists?(:author_name => "David") | |||
assert Topic.exists?(:title => "The First Topic") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
@@ -818,13 +819,13 @@ def test_find_one_message_with_custom_primary_key | |||
Toy.primary_key = :name | |||
begin | |||
Toy.find 'Hello World!' | |||
rescue ActiveRecord::RecordNotFound => e | |||
rescue ActiveRecord:RecordNotFound: e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_attribute
in 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)