Method: WeaviateRecord::Base#initialize

Defined in:
lib/weaviate_record/base.rb

#initialize(hash = {}, custom_selected: false, **attributes) ⇒ Base

Creates a new record with the given attributes. The attributes can be passed as a hash or as key value pairs. It does not save the record in the weaviate database.

Example:

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

# => #<Article:0x0000000105468ab0 ... "Hello World", content: "This is the content of the article">

Article.title # => "Hello World"
Article.content # => "This is the content of the article"

Article.title = 'Not Hello World'
Article.title # => "Not Hello World"

Article.persisted? # => false

The custom_selected parameter is used to indicate whether the attributes are custom picked. If the attributes are custom picked, the attribute readers will be defined only for the selected attributes. The record with custom_selected as true cannot be saved, updated, or destroyed.



117
118
119
120
121
122
123
124
125
# File 'lib/weaviate_record/base.rb', line 117

def initialize(hash = {}, custom_selected: false, **attributes)
  attributes_hash = (hash.present? ? hash : attributes).deep_transform_keys(&:to_s)
  @connection = WeaviateRecord::Connection.new(collection_name)
  @custom_selected = custom_selected
  @attributes = {}
  @meta_attributes = attributes_hash['_additional'] || { 'id' => nil, 'creationTimeUnix' => nil,
                                                         'lastUpdateTimeUnix' => nil }
  run_attribute_handlers(attributes_hash)
end