Search code examples
ruby-on-railsactiverecordhstore

Rails, activerecord, Delete a key value pair from hstore record


I have hstore in my model like this:

store_accessor :properties, :a, :b, :c, :d

Let's suppose a record in the database has the hstore column stored like this (note this is not the whole record, just the hstore part shown here):

properties: {"a"=>"1", "b"=>"2", "d"=>"5"}

If I want to delete key and value pair "b"=>"2" so that the result is

properties: {"a"=>"1", "d"=>"5"}

how do I do this in the model code? I tried this:

 update_attribute(:b, nil)

But this sets b to nil. I want to delete the key value pair for b not set it to nil.

thanks!


Solution

  • Try

    update_attribute([%(properties = delete("properties",?)), 'b'])
    

    OR

    properties.delete("b")