Skip to content

Has and belongs to many ID insertions get expensive, fast on Ruby on Rails if you're inserting a lot of joins at once. Use this to remedy that slowness.

License

Notifications You must be signed in to change notification settings

sportngin/m2m_fast_insert

 
 

Repository files navigation

m2m_fast_insert

TravisCI Code Climate Gem Version

Why

When doing a mass ID insert on a simple HABTM join table, Rails will load the entire records of all the IDs you're inserting. That seemed silly to me, since that's not running validations. Its also disgustingly slow if you're inserting, say, 10,000 records.

This Gem solves that issue for now. I didn't want to override the insertion method, so I added #fast_<relation_name>_ids_insert to force a bare SQL call that just drops all of the IDs in without all of the overhead of loading their objects.

Usage

class User < ActiveRecord::Base
  has_and_belongs_to_many :posts
  
  def self.first_and_insert # as an example
    User.first.fast_post_ids_insert [1,2,3,4,5,6,7,8,9,10] # This is the actual call
  end
  
  def insert_lots_of_posts # as an example
    fast_post_ids_insert [1,2,3,4,5,6,7,8,9,10] # This is the actual call
  end
end

This project rocks and uses MIT-LICENSE.

TODO

  • Allow options for how hard to fail with missing ID or ID params
  • Fall back to the rails-y insert on an empty ID?

About

Has and belongs to many ID insertions get expensive, fast on Ruby on Rails if you're inserting a lot of joins at once. Use this to remedy that slowness.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%