Not a man was left in Ai or Bethel who did not go out after Israel. They left the city open and pursued Israel.
Joshua 8:17

God protected the families that the men of Gad, Reuben, and Manasseh left east of the Jordan because they trusted in him.

The men of Ai trusted in their gods to protect their families too, but that didn't work out as well. The Aians didn't understand that their gods were created and/or imaginary beings just as subject to YHWH as Israel was or they never would have resisted.