Implemented create_ot_update() to all models.
This commit is contained in:
parent
f8c11955ee
commit
267d153f2d
1 changed files with 17 additions and 0 deletions
|
@ -50,4 +50,21 @@ class ApplicationModel < ActiveRecord::Base
|
|||
end
|
||||
self.create(data)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue