如何通过 ActiveRecord 将 Rails 3.1.x 数据库合并到标准 ruby 应用程序中?
我有一个 Rails 3.1.x 应用程序,需要通过另一个直接的 ruby 应用程序来操作其数据库中的数据。本质上,我只是从数据库中的模型检索数据。
显然,它会从以下内容开始:
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:encoding => 'utf8',
:database => 'database_name',
:username => 'username',
:password => 'password',
:host => 'hostname'
)
但是模型已经设置完毕,我该从哪里开始呢?我是否导入 schema.rb
或模型?
希望有一个更全面的用例示例,将 Rails 中使用的数据库放入标准 ruby 脚本中。
I have a Rails 3.1.x app and need to manipulate the data in it's database via another straight ruby app. Essentially I am just retrieving data from a model in the database.
Obviously it would start off with something like:
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:encoding => 'utf8',
:database => 'database_name',
:username => 'username',
:password => 'password',
:host => 'hostname'
)
But where do I go from here with models already set up? Do I import the schema.rb
or models?
Would appreciate a more comprehensive use case example of getting a db used in Rails into a standard ruby script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Rails 有 rake 和 script/runner。两者都可以让您轻松访问模型并透明地连接到数据库。
当我从远程主机检索数据文件并需要将其插入 Rails 前端下方的数据库时,我使用了 script/runner。效果非常好。
仔细阅读其中一些 Stack Overflow 问题以获取更多信息:
Rails has rake and script/runner. Both can make it easy to access your models and transparently connect to the database.
I've used script/runner when I was retrieving data files from a remote host and needed to insert it into my database underneath the Rails front-end. It worked very nicely.
Read through some of these Stack Overflow questions for more info:
您将需要模型文件。仅当您要使用迁移时,
schema.rb
才相关,并且将它们拆分到两个不同的应用程序中可能不是一个好主意。使用 Rails 应用程序作为主应用程序,使用新的 Ruby 应用程序作为从属应用程序。只要确保您的模型有任何依赖项,这些依赖项也包含在内。
在某些情况下,如果它们充分捆绑在一起,那么在 Rails 存储库中创建另一个目录来托管其他 Ruby 应用程序可能会更容易。
You'll need the model files. The
schema.rb
is only relevant if you'll be using migrations and it's probably a bad idea to split those across two different applications. Use your Rails app as your master and your new Ruby app as a dependent application.Just be sure that if your models have any dependencies those are included as well.
In some instances it might be easier to create another directory in your Rails repository for hosting this other Ruby application if they are sufficiently tied together.
我以前做过这个,1 个 Sinatra 应用程序和 5 个 Rails 应用程序。
然而我的方法需要 git 和 git 子模块。
将 /models 目录移动到具有自己的存储库的新项目中。
要求该项目作为每个想要使用它的应用程序中的子模块(映射到 /models)。
这意味着您只需要编辑模型的一个版本即可更新使用它们的所有应用程序。
GitHub 有一个关于子模块的非常好的指南:
http://help.github.com/submodules/
I've done this before, 1 x Sinatra app and 5 x rails apps.
However my approach requires git and git submodule.
Move the /models dir into a new project with its own repo.
Require that project as a submodule (mapped to /models) within each app which wants to use it.
This then means that you only ever need to edit one version of the models to update all applications which use them.
GitHub has a pretty good guide on submodule:
http://help.github.com/submodules/