I have a Rails task: should I use script/runner or rake?

后端 未结 8 596
萌比男神i
萌比男神i 2020-12-07 16:08

For ad hoc Rails tasks we have a few implementation alternatives, chief among which would seem to be:

script/runner some_useful_thing
8条回答
  •  星月不相逢
    2020-12-07 16:59

    Passing parameters to a rake task is a pain in the butt, to say the least. You either need to resort to environment variables or a very hackish parameter system that is not intuitive and has lots of caveats.

    If your task needs to handle command line arguments gracefully then writing a script is the way to go.

    Luke Francl mentions script/runner booting up Rails. That's true. But if you don't want to boot up rails then just run the script as is without script/runner. So the only real difference between scripts and rake tasks are their aesthetics. Choose whatever feels right to you.

    I use rake tasks for little tasks (one or two lines). Anything more complicated goes into the script/ directory. I'll break this rule if I think other developers will expect the code to live in one place over another.

提交回复
热议问题