The simplest way is with rails runner because you don’t need to modify your script.
runner runs Ruby code in the context of Rails non-interactively.
https://guides.rubyonrails.org/command_line.html#bin-rails-runner
Just say rails runner script.rb