Ruby Gem Development - How to Use Activerecord

Ruby Gem Development - How to use ActiveRecord?

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.

How to share activerecord models via Ruby Gem

I remember having the same issue with my ecommerce project and later I moved it to separate repository. By simply moving the whole repetitive code to a rails engine.

There are couple of guides available which you can follow:

  1. http://edgeguides.rubyonrails.org/engines.html

  2. http://coding.smashingmagazine.com/2011/06/23/a-guide-to-starting-your-own-rails-engine-gem/

  3. http://railscasts.com/episodes/277-mountable-engines

You can create a generator to install your migration files of your models. So, whenever you mount your rails engine to any rails application and run generators. You'll be up and running with just few commands like: rake db:migrate etc..

Well, what I shared is my experience. I do not know if there is any other better way of doing it.

P.S.: Here is the link to repository of engine I created - https://github.com/suryart/spree_active_sale, maybe code there can help you as a reference.

Trouble connecting to database in standalone ActiveRecord app

I do this also, but I don't use rake, I just use pure ruby. Here are a couple of methods from my project where I use Active record outside of rails.

Basically I connect to the database, check to see if the tables exist, and if not I load the schema file. Once the schema is loaded, I just use normal ActiveRecord methods to populate the database and do queries. I have no need to do migrations so I have not tried that

class MyDb
attr_reader :file_name
def initialize(file_name)
@file_name = file_name
end

def connect()
@connection = ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: filename,
)
create_tables unless tables_exists?
end

def tables_exists?
ActiveRecord::Base.connection.tables.size > 0
end

def create_tables()
puts "database is empty...\ncreating tables"
path_to_current_file = File.expand_path(File.dirname(__FILE__))
load("#{path_to_current_file}/ar_support/schema.rb")
end
end

Then from inside a top level ruby script you can do something like.

my_db = MyDb.new("mydb.sqlite3")
my_db.connect

Here is an example schema.rb file that I also use.

ActiveRecord::Schema.define(version: 20180430230151) do

create_table "chips", force: :cascade do |t|
t.string "name"
end

create_table "macros", force: :cascade do |t|
t.string "name"
t.integer "chip_id"
t.index ["chip_id"], name: "index_macros_on_chip_id"
end

create_table "measurements", force: :cascade do |t|
t.string "name"
t.float "value"
t.string "group_name"
t.string "plot"
t.boolean "test_result"
t.string "test_expression"
t.integer "run_id"
t.index ["run_id"], name: "index_measurements_on_run_id"
end

create_table "runs", force: :cascade do |t|
t.string "source_dir"
t.string "source_file"
t.integer "macro_id"
t.string "corner_name"
t.string "sim_type"
t.string "netlist_type"
t.index ["macro_id"], name: "index_runs_on_macro_id"
end

create_table "tags", force: :cascade do |t|
t.string "name"
t.integer "run_id"
t.string "value"
t.index ["run_id"], name: "index_tags_on_run_id"
end

end

Start using the database. (You also need to define your models)

chip = Chip.find_or_create_by(name: e.chip)
macro = chip.macros.find_or_create_by(name: e.macro)

Why does including a gem in Gemfile resolve a railtie issue, even though this same gem is already included in Gemfile.lock?

This is related to how gems are loaded by bundler. Bundler.require requires gems listed in Gemfile but does not require its dependecy. Its upto the library to require/load its dependency.

The mentioned issue happens when omniauth is not added explicitly to Gemfile, hence bundler does not require it.

But since omniauth-rails_csrf_protection assumes the ominauth is already required, it errors out when user only adds omniauth-rails_csrf_protection but does not add omniauth to Gemfile.

I have created a possible fix for the issue https://github.com/cookpad/omniauth-rails_csrf_protection/pull/13

UPDATE: The is fix has been merged in the gem repo.



Related Topics



Leave a reply



Submit