Pass ruby script file to rails console

后端 未结 7 1068
醉梦人生
醉梦人生 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条回答
  • 2021-01-29 21:36

    Consider creating a rake task.

    For code that I need to create records or support migrations, for example, I often create a rake task like that from this answer. For example:

    In lib/tasks/example.rake:

    namespace :example do
        desc "Sample description you'd see if you ran: 'rake --tasks' in the terminal"
        task create_user: :environment do
            User.create! first_name: "Foo", last_name: "Bar"
         end
    end
    

    And then in the terminal run:

    rake example:create_user
    
    0 讨论(0)
提交回复
热议问题