from my experience I am putting code into Service file as service pattern.
so in my rails app I have something like:
app models services some_super_cool_task.rb
which be executed from rake task, sidekiq worker or direclty in app (controller action etc).
Best thing from this aproach is that you can test that code.
Rake task is untestable