m2m_fast_insert 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/README.markdown +27 -0
- data/lib/m2m_fast_insert/base.rb +1 -2
- data/lib/m2m_fast_insert/version.rb +1 -1
- data/spec/m2m_fast_insert/base_spec.rb +2 -2
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -1,5 +1,32 @@
|
|
1
1
|
# M2mFastInsert
|
2
2
|
|
3
3
|
[](http://travis-ci.org/jphenow/m2m_fast_insert "Travis-CI M2MFastInsert")
|
4
|
+
[Ruby Gems](https://rubygems.org/gems/m2m_fast_insert)
|
5
|
+
|
6
|
+
# Why
|
7
|
+
|
8
|
+
When doing a mass ID insert on a simple HABTM join table, Rails will load the entire records of all the IDs
|
9
|
+
you're inserting. That seemed silly to me, since that's not running validations. Its also disgustingly slow
|
10
|
+
if you're inserting, say, 10,000 records.
|
11
|
+
|
12
|
+
This Gem solves that issue for now. I didn't want to override the insertion method, so I added
|
13
|
+
`#fast_<relation_name>_ids_insert` to force a bare SQL call that just drops all of the IDs in without all
|
14
|
+
of the overhead of loading their objects.
|
15
|
+
|
16
|
+
# Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
has_and_belongs_to_many :posts
|
21
|
+
|
22
|
+
def self.first_and_insert # as an example
|
23
|
+
User.first.fast_post_ids_insert [1,2,3,4,5,6,7,8,9,10] # This is the actual call
|
24
|
+
end
|
25
|
+
|
26
|
+
def insert_lots_of_posts # as an example
|
27
|
+
fast_post_ids_insert [1,2,3,4,5,6,7,8,9,10] # This is the actual call
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
4
31
|
|
5
32
|
This project rocks and uses MIT-LICENSE.
|
data/lib/m2m_fast_insert/base.rb
CHANGED
@@ -3,8 +3,7 @@ module M2MFastInsert
|
|
3
3
|
attr_reader :ids, :options, :id, :join_table, :join_column_name, :table_name
|
4
4
|
|
5
5
|
def initialize(id, join_column_name, table_name, join_table, *args)
|
6
|
-
@ids = args[0]
|
7
|
-
raise TypeError, "IDs must be fixnums" if ids.any? { |i| !i.is_a? Fixnum }
|
6
|
+
@ids = args[0].collect(&:to_i)
|
8
7
|
@options = args[1].present? ? args[1] : {}
|
9
8
|
@ids.uniq! if options[:unique]
|
10
9
|
@id = id
|
@@ -38,11 +38,11 @@ module M2MFastInsert
|
|
38
38
|
end
|
39
39
|
|
40
40
|
describe "basic init" do
|
41
|
-
let(:ids) { [
|
41
|
+
let(:ids) { [1,2,Base] }
|
42
42
|
it "denies non-fixnum ids" do
|
43
43
|
expect {
|
44
44
|
Base.new(id, join_column_name, table_name, join_table, ids)
|
45
|
-
}.to raise_error(
|
45
|
+
}.to raise_error(NoMethodError)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: m2m_fast_insert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -160,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: '0'
|
161
161
|
segments:
|
162
162
|
- 0
|
163
|
-
hash: -
|
163
|
+
hash: -1507217789133467603
|
164
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
165
|
none: false
|
166
166
|
requirements:
|
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
169
|
version: '0'
|
170
170
|
segments:
|
171
171
|
- 0
|
172
|
-
hash: -
|
172
|
+
hash: -1507217789133467603
|
173
173
|
requirements: []
|
174
174
|
rubyforge_project: m2m_fast_insert
|
175
175
|
rubygems_version: 1.8.24
|