Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#6021

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.

Reported by Prem Sichanugrist (sikachu) · November 20th, 2010 @ 05:40 PM

State: stale
Milestone: 3.x
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Ryan Bigg
    Ryan Bigg

    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.

    November 23rd, 2010 @ 04:13 AM

  2. Jason Langenauer
    Jason Langenauer

    +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.

    November 23rd, 2010 @ 04:38 AM

  3. rails
    rails

    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.

    February 24th, 2011 @ 12:00 AM

  4. rails
    rails
    • State changed from open to stale

    February 24th, 2011 @ 12:00 AM