The ruling in New Hampshire came in response to a legal challenge by two of the largest teachers unions in the US.