Coder Social home page Coder Social logo

jonkarna / m2m_fast_insert Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jphenow/m2m_fast_insert

0.0 2.0 0.0 110 KB

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: MIT License

Ruby 100.00%

m2m_fast_insert's Introduction

M2mFastInsert

TravisCI 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?

m2m_fast_insert's People

Contributors

jphenow avatar jonkarna avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.