depends on how wide it is, but assuming that it has a 3-4" diameter then don't put the screws straight through the wood into it put them into the rail at a 45
° angle, alternating between left and right. So you have the rail on top of one 2x4 and screw through the 2x4 into the rail at diagonals every foot or so. Did mine that way, hasn't come loose yet as the alternating diagonals hold it in place.
I don't think that bolts will work as it will be hard to put them in after the first foot or so.