Use named_scope to add elegant finders to your models

I used to be a fan of the has_finder gem written by Nick Kallen.

Now with Rails 2.1, this functionnality is built-in so you don’t need to install the gem.

The named_scope method allows you to add finders to any model. A code snippet is worth 10000 words :

Doing this you can chain expressions like this :

Maybe you knew it, maybe you didn’t. If you didn’t, now you know it. (I feel very smart about this last sentence. I think I am going to put this in my room somewhere)

  • http://beginrescue.blogspot.com pete

    Minor nitpick: named_scope was introduced in Railes 2.1, so anyone using Rails 2.0.2 (like me) still has to use has_finder.

  • http://www.rubyfleebie.com Frank

    Thanks for the precision Pete. I’m going to update the original post.

  • Pingback: 使用named_scope在Model中添加finder方法 - IceskYsl@1sters!()

  • http://www.youtube.com/watch?v=A8-P1Td-SQ0 futuroscope

    I was suggested this website by my cousin. I’m not sure
    whether this post is written by him as nobody else know such
    detailed about my difficulty. You are amazing! Thanks!

  • https://Youtube.com/watch?v=zJZfREfzP6I psychic attack

    It’s awesome to pay a visit this web page and reading the views of all friends about this piece of writing, while I am also
    keen of getting experience.

  • http://Drag-racing-hack.com/ Drag Racing Hack

    I was wondering if you ever thought of changing
    the structure of your site? Its very well written; I love what
    youve got to say. But maybe you could a little more in the way
    of content so people could connect with it better. Youve got an awful lot of text for only having 1 or
    2 images. Maybe you could space it out better?

  • https://www.youtube.com/watch?v=w5o2PfWeFQ0 Eternity Warriors 2 Hack

    When someone writes an post he/she retains the thought of a user
    in his/her brain that how a user can understand it.

    Therefore that’s why this post is outstdanding. Thanks!

  • http://www.youtube.com/watch?v=aCkwHDkzSY4 UberStrike Hack

    Superb site you have here but I was wondering if you
    knew of any discussion boards that cover the same topics discussed here?
    I’d really love to be a part of group where I can get suggestions from
    other experienced people that share the same interest.
    If you have any recommendations, please let me know.

    Bless you!

  • https://www.youtube.com/watch?v=gAytHrvzBbQ Megapolis Hack

    I was suggested this website by my cousin. I’m not sure whether this post is written by him as no one else
    know such detailed about my difficulty. You’re amazing! Thanks!

  • https://www.youtube.com/watch?v=oQzHDowsKT8 Galaxy Life Hack

    I got this website from my pal who informed me concerning this web site and at the moment this time
    I am browsing this web site and reading very informative articles or reviews here.

  • https://www.youtube.com/watch?v=Shgjt69lC-A Car Town Hack

    That is a really good tip particularly to those fresh to the blogosphere.
    Simple but very precise info… Many thanks for sharing this one.
    A must read post!

  • http://my-free-zoo-hack.com/ My Free Zoo Hack

    Very nice article. I definitely love this site. Keep writing!

  • https://www.youtube.com/watch?v=pes1Z_Vlme8 Warframe Hack

    After I initially left a comment I appear to have clicked the
    -Notify me when new comments are added- checkbox and now each time
    a comment is added I receive four emails with the same comment.
    Is there a means you are able to remove me from that service?
    Many thanks!

  • http://hemel.ro/comments/feed anti

    It’s a shame you don’t have a donate button! I’d definitely donate
    to this outstanding blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will share this
    blog with my Facebook group. Talk soon!