A Rails HOWTO: Query By Example
So I was hanging out in #rubyonrails yesterday, and someone asked a great question:
(10:40:53) jbwiv: guys, does Rails have the equivalent of hibernate's query by example?
(10:41:39) jbwiv: By QBE, I mean I can create an object, set fields on that object, and then query the database by providing that object as an example. Does rails (active record) have an equivalent?
The answer is interesting: no, but adding that’s a snap.
Wanna see how?
Update 2/19/2006: find_by_example now takes arrays as parameters.
Update 2/6/2006: One of the folks in #rubyonrails pointed out a possible security problem with this, if the attributes list is taken directly from the user. Sanitization has been added.
7 comments »