require 'cgi'

class Hash
  def to_query
    query = []
    self.each_pair do |key,value|
      query << "#{key}=#{CGI::escape(value.to_s)}"
    end
    query.join('&')
  end
end

require 'cgi'

class Hash
  def to_query
    attributes = keys.dup
    attributes.sort!{|a,b| a.to_s <=> b.to_s}
    attributes.map!{|k| "#{k}=#{CGI::escape(self[k].to_s)}"}
    attributes.join('&')
  end
end
Make sure the query string is always in the same order.
Make sure the query string is always in the same order.
