From 52b9eb1d8dea2615a8ff6182278dc53e1cf33c1d Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Mon, 1 Mar 2010 12:40:56 -0300 Subject: Prevent from getting errors when trying to get primary key value. --- lib/arel/engines/sql/engine.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib/arel/engines') diff --git a/lib/arel/engines/sql/engine.rb b/lib/arel/engines/sql/engine.rb index 6087a26816..8accf3874c 100644 --- a/lib/arel/engines/sql/engine.rb +++ b/lib/arel/engines/sql/engine.rb @@ -26,13 +26,13 @@ module Arel module CRUD def create(relation) - primary_key_value = nil - if primary_key = relation.primary_key - if primary_key_attribute_and_value = relation.record.detect{|k, v| k.name.to_s == primary_key.to_s} - primary_key_value = primary_key_attribute_and_value[1].value + attribute = [*relation.record].map do |attr, value| + if attr.respond_to?(:name) && !relation.primary_key.blank? && attr.name == relation.primary_key + value end - end - connection.insert(relation.to_sql(false), nil, primary_key, primary_key_value) + end.compact.first + primary_key_value = attribute ? attribute.value : nil + connection.insert(relation.to_sql(false), nil, relation.primary_key, primary_key_value) end def read(relation) -- cgit v1.2.3