Ruby Gem开发-如何使用ActiveRecord? [英] Ruby Gem Development - How to use ActiveRecord?
问题描述
我目前正在尝试开发我的第一颗红宝石宝石,但已经被卡住了。我使用 bundle gem命令创建了基本结构,并阅读了一些教程,但我找不到如何集成ActiveRecord的方法。
I'm currently trying to develop my first ruby gem and I'm already stuck. I used the "bundle gem" command to create the basic structure and read some tutorials but what I can't find is how to integrate ActiveRecord.
在哪里创建我的
我是在lib文件夹中还是在根目录中创建 db / migrations文件夹?
Do I create the "db/migrations" folder within the lib folder or at the root?
我是否必须在Rakefile中做任何事情(我发现了一些问题,答案是您必须创建自己的[my_gem]:db:migrate之类的东西。)
And do I have to do anything in the Rakefile (I found some questions where the answer was something like "you have to create your own [my_gem]:db:migrate" or something like that.)
我所需要的就是创建一个gem的方法,该gem定义ActiveRecord模型(当然包括迁移),然后可以由rails应用程序使用。
All I need is a way to create a gem, which defines ActiveRecord models (including the migrations of course), which can then be used by a rails app.
对此的任何帮助将不胜感激!
Any help on that one would be greatly appreciated!
问候,弗洛尔
推荐答案
在构建与Rails项目集成的gem时,您需要构建Railtie引擎。如果您使用的是Rails 3.0.x,请使用 enginex ;如果您使用的是Rails 3.1,则应使用新生成器:
When building a gem to integrate with a rails project, you want to build a railtie engine. If you are using rails 3.0.x, use enginex, if you are using rails 3.1 you should be use the new generator:
rails g plugin new your-plugin-name
然后,在您的gem内,您只需在 app / models /
文件夹内定义模型,即可
Then, inside your gem, you can just define models, inside the app/models/
folder and they will automatically be picked up.
迁移会更加困难:对于rails 3.1,可以在正确的文件夹中定义它们,在rails 3.0中,您必须手动生成将迁移复制到代码库的任务。检查此链接我在其中回答了这个问题。
Migrations is somewhat harder: for rails 3.1 it is ok if you define them in the correct folder, in rails 3.0 you will have to manually generate a task to copy the migrations to your code-base. Check this link where I answered that very question.
For more information on rails engines check this and this article.
这篇关于Ruby Gem开发-如何使用ActiveRecord?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!