@@ -8,15 +8,16 @@ class InsertAll # :nodoc:
8
8
attr_reader :on_duplicate , :update_only , :returning , :unique_by , :update_sql
9
9
10
10
class << self
11
- def execute ( model , ...)
12
- model . with_connection do |c |
13
- new ( model , c , ...) . execute
11
+ def execute ( relation , ...)
12
+ relation . model . with_connection do |c |
13
+ new ( relation , c , ...) . execute
14
14
end
15
15
end
16
16
end
17
17
18
- def initialize ( model , connection , inserts , on_duplicate :, update_only : nil , returning : nil , unique_by : nil , record_timestamps : nil )
19
- @model , @connection , @inserts = model , connection , inserts . map ( &:stringify_keys )
18
+ def initialize ( relation , connection , inserts , on_duplicate :, update_only : nil , returning : nil , unique_by : nil , record_timestamps : nil )
19
+ @relation = relation
20
+ @model , @connection , @inserts = relation . model , connection , inserts . map ( &:stringify_keys )
20
21
@on_duplicate , @update_only , @returning , @unique_by = on_duplicate , update_only , returning , unique_by
21
22
@record_timestamps = record_timestamps . nil? ? model . record_timestamps : record_timestamps
22
23
@@ -31,10 +32,8 @@ def initialize(model, connection, inserts, on_duplicate:, update_only: nil, retu
31
32
@keys = @inserts . first . keys
32
33
end
33
34
34
- if model . scope_attributes?
35
- @scope_attributes = model . scope_attributes
36
- @keys |= @scope_attributes . keys
37
- end
35
+ @scope_attributes = relation . scope_for_create . except ( @model . inheritance_column )
36
+ @keys |= @scope_attributes . keys
38
37
@keys = @keys . to_set
39
38
40
39
@returning = ( connection . supports_insert_returning? ? primary_keys : false ) if @returning . nil?
@@ -74,7 +73,7 @@ def update_duplicates?
74
73
def map_key_with_value
75
74
inserts . map do |attributes |
76
75
attributes = attributes . stringify_keys
77
- attributes . merge! ( scope_attributes ) if scope_attributes
76
+ attributes . merge! ( @ scope_attributes)
78
77
attributes . reverse_merge! ( timestamps_for_create ) if record_timestamps?
79
78
80
79
verify_attributes ( attributes )
@@ -99,8 +98,6 @@ def keys_including_timestamps
99
98
end
100
99
101
100
private
102
- attr_reader :scope_attributes
103
-
104
101
def has_attribute_aliases? ( attributes )
105
102
attributes . keys . any? { |attribute | model . attribute_alias? ( attribute ) }
106
103
end
0 commit comments