Max Hallum
Max Hallum

Reputation: 25

Does Rails maintain ordering for multiple URL array parameters

I've got multiple lists that I'd like to pass through to a Rails service as parameters in a GET request: "num[]=1&let[]=a&num[]=2&let[]=b&num[]=3&let[]=c"

On the rails end, I'd like to receive

#Good!!!
nums = params[:num] #[1,2,3]
lets = params[:let] #[a,b,c]

and not

#Bad....
nums = params[:num] #[1,2,3]
lets = params[:let] #[c,b,a]

So that '1' can be associated with 'a', '2' can be associated with 'b', etc. Is there any guarantee that Rails will keep the ordering that URL array elements are received in?

Upvotes: 2

Views: 598

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49890

Param parsing is actually handled by rack and not by rails. Array parameters are appended in the order they are received, so yes in current versions the order will be maintained. I don't believe there is a formal standard for nested object passing through query parameters and it doesn't appear to be specifically documented in rack either. So, yes it works currently, doesn't appear to be a guarantee it will always work like that. Here's a short article that talks about how the params parsing works http://codefol.io/posts/How-Does-Rack-Parse-Query-Params-With-parse-nested-query

Upvotes: 3

kimrgrey
kimrgrey

Reputation: 562

Yep, sure. It's an arrays, so it will be in the same order like it was specified in URL.

Upvotes: 1

Related Questions