views:

425

answers:

1

I want 'logical OR' dynamic finders in ActiveRecord. Anybody seen such a thing?

So in the spirit of something like this..

User.find_by_name_and_email("foo", "[email protected]")

.. you could do stuff like this..

User.find_by_username_or_email(user_input)
+4  A: 

How often are you really going to have fields in your database that will have ientical values that you can look up like that? Enough to worry about using a dynamic finder method? Probably not. What's wrong with a named_scope for this?

class User < ActiveRecord::Base

  named_scope :user_or_email, lambda{ |user_name|
    { :conditions => ["username =? OR email =?", user_name, user_name] }
  }

end

That does what you want, and I just don't think this kind of thing will pop up often enough to justify dynamic finders for it, but hey I could be wrong.

railsninja
Good call. Thanks!
Zeke