From 02f5cf76dfe4a618355f8edff76950f28e9670b3 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 12 Jan 2015 08:31:30 +0100 Subject: [PATCH] Added :id support to create_or_update method. --- app/models/application_model.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/application_model.rb b/app/models/application_model.rb index 46b02da6b..d91a1b81a 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -358,7 +358,7 @@ returns =begin -create or update model (check exists based on name, login or locale) +create or update model (check exists based on id, name, login or locale) result = Model.create_or_update( attributes ) @@ -369,7 +369,16 @@ returns =end def self.create_or_update(data) - if data[:name] + if data[:id] + records = self.where( :id => data[:id] ) + records.each {|record| + record.update_attributes( data ) + return record + } + record = self.new( data ) + record.save + return record + elsif data[:name] records = self.where( :name => data[:name] ) records.each {|record| if record.name == data[:name]