0

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!

GregF
  • 143
  • 1
  • 13
  • 1
    I think using except method on the 'properties' and then saving the record will help. Try this, but modify according to your needs. -> [http://stackoverflow.com/questions/6227600/how-to-remove-a-key-from-hash-and-get-the-remaining-hash-in-ruby-rails](http://stackoverflow.com/questions/6227600/how-to-remove-a-key-from-hash-and-get-the-remaining-hash-in-ruby-rails) – Vasanth Mar 16 '17 at 06:16

1 Answers1

3

Try

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

OR

properties.delete("b")
puneet18
  • 4,341
  • 2
  • 21
  • 27