Method: ModelRecord#update

Defined in:
lib/model/the_record.rb

#update(set: {}, remove: {}, **args) ⇒ Object

Convenient update of the dataset

A) Using PATCH

Previously changed attributes are saved to the database.

Using the optional »:set:« argument ad-hoc attributes can be defined

V.create_class :contracts
obj = Contracts.first
obj.name =  'new_name'
obj.update set: { yesterdays_event: 35 }

updates both, the »name« and the »yesterdays_event«-properties

B) Manual Modus

Update accepts a Block. The contents are parsed to »set«. Manual conversion of ruby-objects to the database-input format is necessary

i.e. hct is an Array of ActiveOrient::Model-records. then obj.update { “positions = #ModelRecord.hcthct.to_or ” } translates to update #83:64 set positions = [#90:18, #91:18, #92:18] return after @this and returns the modified record.

The manual modus accepts the keyword »remove«.

obj.update(remove: true) { “positions = #ModelRecord.hcthct.firsthct.first.to_or ” } translates to update #83:64 remove positions = #90:18 return after @this

This can be achieved by

obj.positions

If the update process is not successful, nil is returned



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/model/the_record.rb', line 171

def update set: {}, remove: {}, **args
	logger.progname = 'ActiveOrient::Model#Update'
	#	query( kind: update,  )
	if block_given?			# calling vs. a block is used internally
		# to remove an Item from lists and sets call update(remove: true){ query }
		set_or_remove =  args[:remove].present? ? "remove" : "set"
		#transfer_content from: 	 
		updated_record = 	db.execute{  "update #{rrid}  #{ yield }  return after $current" } &.first
		transfer_content from: updated_record  if updated_record.present?
	else
		set = if remove.present?
						{ remove: remove.merge!( args) }
					elsif set.present?
						 set.merge!( args) 
					else
						 args 
					end
		#	set.merge updated_at: DateTime.now
		if rid.rid?
			q= query.kind(:update)
			if remove.present?
				q.remove(remove)
			else
				q.set(set)
			end
		transfer_content from: 	q.execute(reduce: true){ |y| y[:$current].reload! }
		else  # new record
			 self.attributes.merge!  set
			 save
		end
	end
end