class Hash

def to_query(namespace = nil)

This method is also aliased as +to_param+.

are sorted lexicographically in ascending order.
The string pairs "key=value" that conform the query string

# => "user[name]=David&user[nationality]=Danish"
{name: 'David', nationality: 'Danish'}.to_query('user')

An optional namespace can be passed to enclose the param names:

# => "name=David&nationality=Danish"
{name: 'David', nationality: 'Danish'}.to_query

query string:
Returns a string representation of the receiver suitable for use as a URL
def to_query(namespace = nil)
  collect do |key, value|
    value.to_query(namespace ? "#{namespace}[#{key}]" : key)
  end.sort * '&'
end