Method: WeaviateRecord::Base#update

Defined in:
lib/weaviate_record/base.rb

#update(hash = {}, **attributes) ⇒ Object

Updates the record in the weaviate database. Returns true if the record is updated successfully. Otherwise, it returns false. If the record is not updated successfully, it adds the errors to the record.

Example:

class Article < WeaviateRecord::Base
 validates :title, presence: true
end

article = Article.new(title: 'Hello World', content: 'This is the content of the article')
article.save

article.update(title: 'Not Hello World') # => true
article.title # => "Not Hello World"

article.update(title: '') # => false
article.errors.full_messages # => ["Title can't be blank"]

If you try to update the meta attribute, it will raise an error

Example:

article.update(id: 'new_id')
# => WeaviateRecord::Errors::MetaAttributeError: 'cannot update meta attributes'


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/weaviate_record/base.rb', line 185

def update(hash = {}, **attributes)
  attributes_hash = (hash.present? ? hash : attributes).deep_transform_keys(&:to_s)
  validate_record_for_update(attributes_hash)
  merge_attributes(attributes_hash)
  return false unless valid?

  result = @connection.update_call(@meta_attributes['id'], @attributes)
  raise WeaviateRecord::Errors::ServerError, 'unable to update the weaviate record' unless result.is_a?(Hash)

  errors.add(:base, message: result['error']) if result['error'].present?
  result['id'].present?
end