(1) Create a hash from query parameters. Sort the keys if needed.

r = Hash[request.query_parameters.sort]

(2) Loop the hash to reconstruct the query string.

query_string = ''
r.each {|k,v| query_string << "#{k}=#{v}&" }

(3) Remove the last & character.

query_string = query_string[0...-1]