Skip to content

Working with Hashes

topofocus edited this page May 30, 2019 · 5 revisions

Hashes are created schemaless just by assigning a Hash to a database-property. Any modification returns the modified hash.

Alternatively they can be defined explicitly

> TestModel.create_property :ll, type: :set
# or
> TestModel.create_property :ll, type: :link_set,
                             linked_class: Contracts

Creation

> the_record =  TestModel.create ll: { tus: 6, tux: 9, zottel: 'rt'}
> the_record.to_human
  <TestModel: ll : {:tus=>6, :tux=>9, :zottel=>"rt"}>

Accessing

> the_record.ll[:tux]            # => 6
> the_record.ll.slice /tu/       # => {:tus=>6, :tux=>9} 

Assignment / Merge

> the_record.ll.merge(zt: 7, bla: [8,9,10] )   # returns the modified hash
> the_record.ll[:zt] = 7                       # returns the assigned value   
  INFO->update #25:0 set ll.zt = 7  return after @this
 => {:tus=>6, :tux=>9, :zottel=>"rt", :zt=>7, bla: [8,9,10] } 

Update

> the_record.ll[:zt] = 'uz'
  INFO->update #25:0 set ll.zt = 'uz'  return after @this
  <TestModel: ll : {:tus=>6, :tux=>9, :zottel=>"rt", :zt=>"uz"}>

Remove / Delete

> the_record.ll.remove :zottel
  INFO->update #25:0 remove ll.zottel   return after @this
  <TestModel: ll : {:tus=>6, :tux=>9, :zt=>"uz"}>

Clone this wiki locally