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

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Hash Conditions To Array Conditions (required By Find_by_tsearch)

07.28.2010
| 6562 views |
  • submit to reddit
        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

Legal Steroids and Bodybuilding Supplements are the one and only specialty of winni Juice has been established as a respectable and valuable source of education and information since 1999