Creating you own find methods

0 views
Skip to first unread message

Gman

unread,
Feb 26, 2008, 10:57:52 AM2/26/08
to Cape Town Ruby Brigade
Hey guys

I often come across when working with other people that custom find
methods are created for your models in order to make you controllers
more readable and more understandable, examples : User.find_admin,
News.find_latest, etc

But when you use these methods they should not work like this :
News.find_latest(20)
User.find_admin

I believe these methods should always follow the default find method
convention, ie you should always be able to do things like this :
News.find_latest(:all, :conditions => 'author_id <> 1', :limit => 10)
User.find_admin(:first)

And beside keeping the conventions is so easy :
def find_latest(*args)
with_scope(:find => {:order => 'created_at DESC, id DESC'}) do
find(*args)
end
end

def find_latest(*args)
with_scope(:find => {:conditions => ['admin = ?',true]}) do
find(*args)
end
end

So what do you guys think? Am I just be unnecessary?
Reply all
Reply to author
Forward
0 new messages