简体   繁体   English

通过Rails模型从单独的Ruby脚本访问数据库

[英]Accessing a database through Rails model from a seperate Ruby script

I have a Rails application with a database (PostgreSQL using ActiveRecord) 我有一个带有数据库的Rails应用程序(使用ActiveRecord的PostgreSQL)

In a separate directory on the same server I have a Ruby script. 在同一服务器上的单独目录中,我有一个Ruby脚本。

How can I, from the Ruby script, reach the Rails database through one of my Rails models? 如何从Ruby脚本通过我的Rails模型之一访问Rails数据库?


I have seen that it is possible to require ActiveRecord and pass in the database details, but from what I understand I would need to rebuild the model, which means a lot of repetition of validations, methods, etc. Instead I'd like to somehow use the model that's already in place. 我已经看到可能需要ActiveRecord并传递数据库详细信息,但是据我了解,我将需要重建模型,这意味着需要重复进行大量的验证,方法等。相反,我想以某种方式进行使用已经存在的模型。

I found a solution that has the behaviour I was looking for, and am posting it as an answer for anyone who comes across this question at a later date. 我找到了具有所需行为的解决方案,并将其发布为以后遇到此问题的任何人的答案。

Setting ENV['RAILS_ENV'] = "production" and then simply requiring the environment.rb file (with the appropriate path) in the Ruby script solves the issue very nicely. 设置ENV['RAILS_ENV'] = "production" ,然后只需要在Ruby脚本中使用environment.rb文件(具有适当的路径)就可以很好地解决该问题。

This is similar to the answer provided by @MurifoX, but saves you having to re-declare the DB connection. 这与@MurifoX提供的答案类似,但是省去了重新声明数据库连接的麻烦。

Try using rails runner. 尝试使用滑轨。 According to: http://guides.rubyonrails.org/command_line.html#rails-runner 根据: http : //guides.rubyonrails.org/command_line.html#rails-runner

runner runs Ruby code in the context of Rails non-interactively. 运行程序在Rails上下文中非交互地运行Ruby代码。 For instance: 例如:

bin/rails runner "Model.long_running_method"

In my experience this works nicely to run scripts that rely on Active Record and more. 以我的经验,这可以很好地运行依赖于Active Record等的脚本。

Add the -e switch to the command to force the use of your production database connection: 将-e开关添加到命令以强制使用生产数据库连接:

bin/rails runner -e staging "Model.long_running_method" 

Add any other environment variables that may be required to complete the database connection. 添加完成数据库连接可能需要的任何其他环境变量。 If you use the Devise gem, you will also need to pass in a secret to allow the Devise initializations to complete. 如果您使用Devise gem,则还需要传递一个秘密,以允许完成Devise初始化。 Other gems may equally need some assistance to get started. 其他宝石同样可能需要一些帮助才能开始。

You can use require to pass the relative path to your model, and then instatiate it a use. 您可以使用require将相对路径传递到模型,然后将其实例化。

require '/path/to/your/model/inside/your/app'

model = Model.new 

For the connection, if you are using postgresql , you can use the PGconn class from the pg gem like this: 对于连接,如果您使用的是postgresql ,则可以使用pg gem中的PGconn类,如下所示:

@connection = PGconn.open(
  :user => 'postgres', 
  :password => 'password', 
  :host => 'host', 
  :dbname => 'dbname')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM