DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Convert Hash Conditions To Array Conditions (required By Find_by_tsearch)
Inspired by http://snippets.dzone.com/posts/show/6839
class Hash
def to_array_conditions
[self.keys.map{|k| "#{k} = ?" }.join(" AND "), self.values].flatten
end
end
test "convert hash conditions to array conditions" do
assert_equal ['city = ? AND country_code = ? AND state = ?',
"Adendorf", "DE", "Niedersachsen"], {
:country_code => "DE",
:state => "Niedersachsen",
:city => "Adendorf"
}.to_array_conditions
end






Comments
Snippets Manager replied on Mon, 2012/03/05 - 3:46am