Skip to content
This repository
Browse code

Refactor thethe wayy to obtain primary key value.

  • Loading branch information...
commit c03d4bb8e4acac95961a1a9eb9578fa33a47da66 1 parent 1994d3e
Emilio Tagua miloops authored

Showing 1 changed file with 7 additions and 6 deletions. Show diff stats Hide diff stats

  1. +7 6 lib/arel/engines/sql/engine.rb
13 lib/arel/engines/sql/engine.rb
@@ -26,12 +26,13 @@ def method_missing(method, *args, &block)
26 26
27 27 module CRUD
28 28 def create(relation)
29   - attribute = [*relation.record].map do |attr, value|
30   - if attr.respond_to?(:name) && !relation.primary_key.blank? && attr.name == relation.primary_key
31   - value
32   - end
33   - end.compact.first
34   - primary_key_value = attribute ? attribute.value : nil
  29 + primary_key_value = if relation.primary_key.blank?
  30 + nil
  31 + elsif relation.record.is_a?(Hash)
  32 + attribute = relation.record.detect { |attr, _| attr.name.to_s == relation.primary_key.to_s }
  33 + attribute && attribute.last.value
  34 + end
  35 +
35 36 connection.insert(relation.to_sql(false), nil, relation.primary_key, primary_key_value)
36 37 end
37 38

0 comments on commit c03d4bb

Please sign in to comment.
Something went wrong with that request. Please try again.