0.0
No commit activity in last 3 years
No release in over 3 years
Fast Inserts for Rails Many to Many relations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
>= 0

Runtime

 Project Readme

M2mFastInsert

MOVED

Moved to https://github.com/sportngin/m2m_fast_insert

Neato links

TravisCI Code Climate Ruby Gems

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?