find_by should return a default

Hi,

I am puzzlin for quite a while on this. I use find_by methods to retieve AR objects. In some cases find_by will return nil if there is no match. However I would like to have find_by return a default object instead.

Is there a way to overload that particular find_by method? Or whats the best way to implement it.

Thanks Jens

I am puzzlin for quite a while on this. I use find_by methods to retieve AR objects. In some cases find_by will return nil if there is no match. However I would like to have find_by return a default object instead.

Is there a way to overload that particular find_by method? Or whats the best way to implement it.

My first thought is that overloading this is very dangerous as you completely lose the ability to hop into the console and run say User.find_by_login('philip') and *get* the 'philip' user or nil. I could see some nasty side affects here.

If it were me I'd either make it explicit in the code "Model.find_by_foo() || Model.get_default()" or extend AR to support find_by_xxx_or_default and then call it that way. Don't forget though that "xxx" might also be "xxx_and_yyy_and_zzz".

-philip