This project is archived and is in readonly mode.

#6021 ✓stale
Prem Sichanugrist (sikachu)

Make User[:id] returning Arel::Attributes::* instance as a shortcut to use ARel's methods

Reported by Prem Sichanugrist (sikachu) | November 20th, 2010 @ 05:40 PM | in 3.x

Hi,

I've discussed with Aaron yesterday about the way that I can use ARel's comparison methods (#gt, #lt, and so on) in AR. Turned out that I have to access it via #arel_table method of the model:

users = User.arel_table
famous_users = User.where(users[:fame].gt(80))

This become a bit tedious as I have to have another variable to store an instance of Arel::Table to be able to call those methods on it. Also, I don't think the code look clean if I merge those two lines together and make it as

famous_users = User.where(User.arel_table[:fame].gt(80))

So, after some discussion, I think it's better to make AR .[] method to return the instance of Arel::Attributes::*. This will give more readability to the code example above, and I think it fits in great with the context.

After applying my patch, you would then can do

famous_users = User.where(User[:fame].gt(80))

I think that looks really nice. I really wish I could get some feedback from you guys.

Comments and changes to this ticket

  • Ryan Bigg

    Ryan Bigg November 23rd, 2010 @ 04:13 AM

    I've been looking for a shortcut for Model.arel_table[:field] for a very long time. It just doesn't make sense to have something like this already.

  • Jason Langenauer

    Jason Langenauer November 23rd, 2010 @ 04:38 AM

    +1

    I've monkey-patched something exactly like this into a Rails application I've been working on. It turns out to be very workable and produces quite succinct code.

  • rails

    rails February 24th, 2011 @ 12:00 AM

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails February 24th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages