Skip to content

Conversation

kbrock
Copy link
Contributor

@kbrock kbrock commented Jun 6, 2016

Summary

Rails know how to convert a column name to an Arel::Attribute.
The rest of the code base uses arel_attribute over Arel::Attribute.new.
This patch cleans up this one spot.

//cc @matthewd was able to remove the aliases code
/cc @sgrif Do you feel this is too private api focused? Any suggestions for a different approach? thnx

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@sgrif
Copy link
Contributor

sgrif commented Jun 7, 2016

Yeah I think this is too private API focused. Overall I'm trying to reduce the amount of places where "an active record class" is the interface desired. column_names is the right thing to be using here, since virtual attributes aren't in the database.

@kbrock
Copy link
Contributor Author

kbrock commented Jun 8, 2016

Overall I'm trying to reduce the amount of places where "an active record class" is the interface desired.

Did you just say that you prefer using column_names over attribute_names?
Or were you talking about avoiding arel_attribute specifically?

virtual attributes aren't in the database.

arel_attribute does it's best to represent an attribute in terms of the database. E.g.: It converts an alias to the proper column name.

So while yes, you don't want to pass a sql expression to arel_attribute, it would be nice to leverage the method's logic for converting things like an attribute alias to a column name. Which will work in the database.

@kbrock kbrock force-pushed the calculate_attributes branch 2 times, most recently from f18160f to b2424c0 Compare September 30, 2016 20:11
@kbrock
Copy link
Contributor Author

kbrock commented Sep 30, 2016

@sgrif Hi again.

I thought pluck [ref] is implemented nicely:

relation.select_values = column_names.map { |cn|
  @klass.has_attribute?(cn) || @klass.attribute_alias?(cn) ? arel_attribute(cn) : cn
}

Used the same logic in aggregate_column

if @klass.has_attribute?(column_name.to_s) || @klass.attribute_alias?(column_name.to_s)
  @klass.arel_attribute(column_name)
else
  Arel.sql(column_name == :all ? "*" : column_name.to_s)
end
  • this makes the logic for the two methods consistent
  • Was able to remove the alias conversion. (I can put back if you want)

kbrock added a commit to kbrock/manageiq that referenced this pull request Sep 30, 2016
the fix in rails is:

rails/rails#25304

But need to find a good way to get that PR merged.
In the mean time, we can support on our side.
@kbrock kbrock force-pushed the calculate_attributes branch from b2424c0 to 27f8f74 Compare October 4, 2016 14:03
@matthewd matthewd merged commit e16b5b4 into rails:master Oct 5, 2016
@kbrock kbrock deleted the calculate_attributes branch October 5, 2016 16:38
jameswnl pushed a commit to jameswnl/manageiq that referenced this pull request Dec 1, 2016
the fix in rails is:

rails/rails#25304

But need to find a good way to get that PR merged.
In the mean time, we can support on our side.
kbrock added a commit to ManageIQ/activerecord-virtual_attributes that referenced this pull request Jan 24, 2019
the fix in rails is:

rails/rails#25304

But need to find a good way to get that PR merged.
In the mean time, we can support on our side.

(transferred from ManageIQ/manageiq@8accb7f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants