Pass ruby script file to rails console

后端 未结 7 1078
醉梦人生
醉梦人生 2021-01-29 20:51

Is there a way to pass ruby file, foo.rb to rails console. Expected results would be after console starts rails environment to run file.

Or any other way which

7条回答
  •  梦毁少年i
    2021-01-29 21:24

    You can use

    bundle exec rails runner "eval(File.read 'your_script.rb')"
    

    UPDATE:

    What we also have been using a lot lately is to load the rails environment from within the script itself. Consider doit.rb:

    #!/usr/bin/env ruby
    
    require "/path/to/rails_app/config/environment"
    
    # ... do your stuff
    

    This also works if the script or the current working directory are not within the rails app's directory.

提交回复
热议问题