The unit is not purchased, it is built. Even the AI does not cheat about buying a unit in a garrisoned city. You can also do shoot with both if you do the same thing. It's a bug that lets you shoot with both range unit, I've done it before so I know it works. What you can not do is attack with a stacked mellee unit.
Also the unit does not move after it fires, it gets booted from the city when the turn ends. Same thing will happen to your unit if you do this, so make sure you have a place for it behind the city or it will die horribly