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

后端 未结 9 1468
醉梦人生
醉梦人生 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:23

    You can open a connection in your scripts as such:

    ActiveRecord::Base.establish_connection(
        :adapter  => "mysql",
        :username => "root",
        :host     => "localhost",
        :password => "******",
        :database => "******" 
    )
    

    I'm sure there is a more elegant way to do it, so that it grabs the info from your database.yml.

提交回复
热议问题