How do I run Ruby tasks that use my Rails models?

后端 未结 9 1341
醉梦人生
醉梦人生 2021-02-01 08:58

I have a Rails app with some basic models. The website displays data retrieved from other sources. So I need to write a Ruby script that creates new instances in my database. I

9条回答
  •  臣服心动
    2021-02-01 09:18

    I'd suggest creating custom rake tasks (lib/task/foo.rake). This give you easy access to most of the functionality of your rails app.

    namespace :foo do
      desc 'do something cool'
      def something_cool
         test = MyModel.new
         test.name = 'test'
         test.save
      end
    end
    

    Then:

    $ rake -T foo
    rake foo:something_cool       # do something cool
    

    You can even run the tasks via a cronjob.

提交回复
热议问题