A federal judge on Friday threw out a lawsuit filed by parents who wanted to keep their young children from learning about gay marriage in school.

U.S. District Judge Mark Wolf said federal courts have decided in other cases that parents' rights to exercise their religious beliefs are not violated when their children are exposed to contrary ideas in school.

"In essence under the Constitution public schools are entitled to teach anything that is reasonably related to the goals of preparing students to become engaged and productive citizens in our democracy," Wolf said in his ruling.

Toni and David Parker of Lexington sued after their 5-year-old son brought home a book from kindergarten that depicted a gay family. Another Lexington couple joined the suit after a second-grade teacher read the class a fairy tale about two princes falling in love.

Both couples claimed Lexington school officials violated their parental rights to teach their own morals to their children. They said they did not want to dictate curriculum but wanted to be notified before gay couples were discussed so they could remove their children from classrooms.

Wolf dismissed both federal and state claims made in the lawsuit but said the parents could refile the lawsuit in state court.

The case attracted attention in part because Massachusetts is the only state that allows same-sex marriage.

Forty-five states ban gay marriage, most of them through voter-approved amendments. New Jersey, Vermont and Connecticut offer civil unions, which offer the protections and benefits of marriage without the title, and California offers domestic partnerships with similar benefits.