Gun purchases are off limits in the U.S. to anyone who uses medical marijuana or holds a state-approved medicinal marijuana card, a federal appeals court ruled Wednesday, accepting the government’s view that illegal-drug users are prone to violence.

Although medical-marijuana use is legal in California, 24 other states and Washington, D.C., the federal government still bans all use of the drug, and in 1968 Congress prohibited illegal-drug users and addicts from buying and possessing firearms.

On Wednesday, the Ninth U.S. Circuit Court of Appeals in San Francisco said the Bureau of Alcohol, Tobacco, Firearms and Explosives properly relied on that law in a September 2011 “open letter” to all licensed gun dealers forbidding sales to anyone who “uses or is addicted to marijuana,” even in states that have legalized the drug for medical purposes.

Federal law still allows some other groups of potentially dangerous people, including those on the government’s “no-fly” list of suspected terrorists, to buy guns.

The ruling was a defeat for a Nevada woman who was turned down for a gun purchase in October 2011 because the store owner knew she had a card allowing her to use medical marijuana, which Nevada had legalized in 2000.

In its 2011 letter, the bureau said a retailer who knows a customer possesses a medical-marijuana card has “reasonable cause to believe” the person is an illegal-drug user who may not buy a gun.

“It is beyond dispute that illegal drug users, including marijuana users, are likely as a consequence of that use to experience altered or impaired mental states that affect their judgment and that can lead to irrational or unpredictable behavior,” the court said in a 3-0 ruling. It was written by Jed Rakoff, a federal judge from New York temporarily assigned to the appeals panel.

Although medical-marijuana users may be less violence-prone than other drug users, Rakoff said, the courts have endorsed the government’s conclusion that “illegal drug users, including marijuana users, are more likely to be involved in violent crimes.”

He also noted that more than half the states have their own laws against gun possession by users of illegal drugs. California prohibits firearms possession by anyone “addicted to the use of any narcotic drug.”

The plaintiff, S. Rowan Wilson, said in her lawsuit that she didn’t actually use the drug, but had obtained the card as a statement of political support for the medical-marijuana movement. But the appeals court, in upholding a federal judge’s ruling, said the government’s need to prevent gun violence outweighed her Second Amendment right to bear arms in self-defense and her First Amendment right of free speech.

Wilson’s lawyer, Charles Rainey, said the court had accepted the government’s misleading explanation of the policy announced in the September 2011 letter by the ATF.

“This is a nonsense policy that really was not aimed at restricting violent people from buying guns, but was aimed strictly at quelling a political movement,” Rainey said.

Representatives of the National Rifle Association weren’t available for comment. On the other side of the gun debate, Hannah Shearer, an attorney at the San Francisco headquarters of the Law Center to Prevent Gun Violence, said the gun restrictions on medical-marijuana users seemed inconsistent with more permissive rules for other groups.

While convicted felons and fugitives from justice are forbidden to own firearms, for example, federal law allows gun ownership by those convicted of violent misdemeanor crimes, other than domestic violence, Shearer said. She said the law also allows mentally ill people to buy guns unless they have been committed to psychiatric institutions or have been judged to be mentally deficient in legal proceedings. And Congress recently rejected legislation supported by the Obama administration that would have outlawed gun purchases by people on the government’s terror watch list.

“It’s heartening to see Congress seriously looking at that (watch-list) issue,” Shearer said. She said the gaps in federal law have been filled by some states, including California, which bans gun possession for those convicted of most types of violent crimes and allows family members to seek a court order removing firearms from a relative who has been acting erratically.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @egelko