Skip to content

Commit 5de9877

Browse files
committed
Check for present returning attributes
1 parent cdda2ca commit 5de9877

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/active_record/returning_attributes/patching.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
6565
sql, binds = sql_for_insert(sql, pk, nil, sequence_name, binds)
6666

6767
exec_query(sql, name, binds).tap do |result|
68-
@_returned_attributes = result.first.to_hash
68+
if @_returning_attributes.present?
69+
@_returned_attributes = result.first.to_hash
70+
end
6971
end
7072
end
7173

7274
def update(arel, name = nil, binds = [])
7375
sql, binds = to_sql_and_binds(arel, binds)
7476

75-
if @_returning_attributes && !@_returning_attributes.empty?
77+
if @_returning_attributes.present?
7678
sql = "#{sql} RETURNING #{@_returning_attributes.map { |column| quote_column_name(column) }.join(', ')}"
7779
end
7880

@@ -95,7 +97,11 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
9597
end
9698

9799
if pk = suppress_composite_primary_key(pk)
98-
sql = "#{sql} RETURNING #{[pk, @_returning_attributes].flatten.map { |column| quote_column_name(column) }.join(', ')}"
100+
sql = if @_returning_attributes.present?
101+
"#{sql} RETURNING #{[pk, @_returning_attributes].flatten.map { |column| quote_column_name(column) }.join(', ')}"
102+
else
103+
"#{sql} RETURNING #{quote_column_name(pk)}"
104+
end
99105
end
100106

101107
super
@@ -120,7 +126,7 @@ def execute_and_clear(sql, name, binds, prepare: false)
120126
result = exec_cache(sql, name, binds)
121127
end
122128

123-
if @_returning_attributes && !@_returning_attributes.empty?
129+
if @_returning_attributes.present?
124130
@_returned_attributes = result.to_a.first
125131
end
126132

0 commit comments

Comments
 (0)