Ruby Gem Development - How to use ActiveRecord?

穿精又带淫゛_ 提交于 2019-12-09 12:35:45

问题


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.

Where do I create my migrations?

Do I create the "db/migrations" folder within the lib folder or at the root?

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.)

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!

Greetings, Flo


回答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

Then, inside your gem, you can just define models, inside the app/models/ folder and they will automatically be picked up.

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.




回答2:


getting the functionality of ActiveRecord can be done by:

require "rubygems"
require "active_record"

class User < ActiveRecord::Base

end

This should work.



来源:https://stackoverflow.com/questions/6938652/ruby-gem-development-how-to-use-activerecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!