7

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

LetzFlow
  • 431
  • 8
  • 17

2 Answers2

7

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.

Community
  • 1
  • 1
nathanvda
  • 49,707
  • 13
  • 117
  • 139
  • 3
    This post is misleading, the question asks about using active record models and migrations from a gem. It is *not* necessary to use Rails to do this. I have a sinatra based web service which I want to gem-ify so all the configuration can be indicated external to the service gem package. This is just one example of many alternate scenarios where assuming rails doesnt work. – johnmartirano Apr 29 '13 at 14:33
  • In the question it clearly states that the gem should define models which can be used in a rails app. – nathanvda Apr 29 '13 at 15:37
  • 1
    "Can" is different from "must". The OP may only have been interested in Rails, but someone else coming along and reading this might be interested in a gem which works with both Rails and Sinatra, for instance. – David Moles Mar 20 '15 at 21:03
  • 1
    I added some clarification that my answer is only applicable to rails development (as asked by the OP, see the presence of the ruby-on-rails tag). When creating a gem for sinatra, you will have to write some more boilerplate, which you already had to do when you enabled migrations in the first place. But on the other hand: you are free to do whatever you like. Define migrations, use a rake task to copy them, define models somewhere in the gem, and require them. There are no generators, there is no init-code. You will have to build all that yourself. – nathanvda Mar 22 '15 at 13:48
  • I'd also reference the Rails guides about plugins at this point: https://guides.rubyonrails.org/plugins.html – Rich Steinmetz Jan 04 '23 at 06:47
3

getting the functionality of ActiveRecord can be done by:

require "rubygems"
require "active_record"

class User < ActiveRecord::Base

end

This should work.

Alok Swain
  • 6,409
  • 5
  • 36
  • 57