Never been to DZone Snippets before?

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) (See related posts)

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 on this post

ertspi5 posts on Dec 21, 2011 at 00:26
Lots of thanks for this post. I think it is a very good post. It helps us many away. So many many thanks for this article.



roof vents
ertspi5 posts on Dec 27, 2011 at 04:44
Hey many time I visited your site and every time I got a very good information in your site.I just say its really very helps full for me.Thanks for sharing this information………



roofers
lailadds posts on Jan 22, 2012 at 02:11
I really salute you webmaster for having this wonderful theme and for sharing your ideas. Hop that you will continue writing more articles than this. Have a great day.

metal roofing

You need to create an account or log in to post comments to this site.


Click here to browse all 7716 code snippets

Related Posts