Implemented create_ot_update() to all models.

This commit is contained in:
Martin Edenhofer 2012-12-30 10:45:01 +01:00
parent f8c11955ee
commit 267d153f2d

View file

@ -50,4 +50,21 @@ class ApplicationModel < ActiveRecord::Base
end end
self.create(data) self.create(data)
end end
def self.create_or_update(data)
if data[:name]
record = self.where( :name => data[:name] ).first
if record
puts 'update ' + data[:name].to_s
record.update_attributes( :data => data[:data] )
else
puts 'create ' + data[:name].to_s
record = self.new( data )
record.save
end
return record
else
raise "Need name for create_or_update()"
end
end
end end