Permalink
Browse files

Refactor thethe wayy to obtain primary key value.

  • Loading branch information...
miloops committed Mar 1, 2010
1 parent 1994d3e commit c03d4bb8e4acac95961a1a9eb9578fa33a47da66
Showing with 7 additions and 6 deletions.
  1. +7 −6 lib/arel/engines/sql/engine.rb
@@ -26,12 +26,13 @@ def method_missing(method, *args, &block)
module CRUD
def create(relation)
- attribute = [*relation.record].map do |attr, value|
- if attr.respond_to?(:name) && !relation.primary_key.blank? && attr.name == relation.primary_key
- value
- end
- end.compact.first
- primary_key_value = attribute ? attribute.value : nil
+ primary_key_value = if relation.primary_key.blank?
+ nil
+ elsif relation.record.is_a?(Hash)
+ attribute = relation.record.detect { |attr, _| attr.name.to_s == relation.primary_key.to_s }
+ attribute && attribute.last.value
+ end
+
connection.insert(relation.to_sql(false), nil, relation.primary_key, primary_key_value)
end

0 comments on commit c03d4bb

Please sign in to comment.