Added :id support to create_or_update method.

This commit is contained in:
Martin Edenhofer 2015-01-12 08:31:30 +01:00
parent d2170259e5
commit 02f5cf76df

View file

@ -358,7 +358,7 @@ returns
=begin =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 ) result = Model.create_or_update( attributes )
@ -369,7 +369,16 @@ returns
=end =end
def self.create_or_update(data) 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 = self.where( :name => data[:name] )
records.each {|record| records.each {|record|
if record.name == data[:name] if record.name == data[:name]