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 :
class Story < ActiveRecord::Base
named_scope :hilarious, :conditions => ["type = ?","comedy"]
named_scope :popular, :conditions => ["popularity_level > ?", 3]
end
Doing this you can chain expressions like this :
funny_stories = @all_stories.hilarious
funny_and_popular = @all_stories.hilarious.popular
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)
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.
Thanks for the precision Pete. I’m going to update the original post.
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!
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.
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?
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!
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!
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!
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.
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!
Very nice article. I definitely love this site. Keep writing!
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!
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!