class ActiveRecord::Base

Adds methods for deleting keys in your hstore columns

Public Class Methods

delete_key(attribute, key) click to toggle source

Deletes all keys from a specific column in a model. E.g.

Person.delete_key(:info, :father)

The SQL generated will be:

UPDATE "people" SET "info" = delete("info",'father');
# File lib/activerecord-postgres-hstore/activerecord.rb, line 10
def self.delete_key attribute, key
  raise "invalid attribute #{attribute}" unless column_names.include?(attribute.to_s)
  update_all([%(#{attribute} = delete("#{attribute}",?)),key])
end
delete_keys(attribute, *keys) click to toggle source

Deletes many keys from a specific column in a model. E.g.

Person.delete_key(:info, :father, :mother)

The SQL generated will be:

UPDATE "people" SET "info" = delete(delete("info",'father'),'mother');
# File lib/activerecord-postgres-hstore/activerecord.rb, line 19
def self.delete_keys attribute, *keys
  raise "invalid attribute #{attribute}" unless column_names.include?(attribute.to_s)
  delete_str = "delete(#{attribute},?)"
  (keys.count-1).times{ delete_str = "delete(#{delete_str},?)" }
  update_all(["#{attribute} = #{delete_str}", *keys])
end

Public Instance Methods

destroy_key(attribute, key) click to toggle source

Deletes a key in a record. E.g.

witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_key(:info, :father)

It does not save the record, so you’ll have to do it.

# File lib/activerecord-postgres-hstore/activerecord.rb, line 30
def destroy_key attribute, key
  raise "invalid attribute #{attribute}" unless self.class.column_names.include?(attribute.to_s)
  new_value = send(attribute)
  new_value.delete(key.to_s)
  send("#{attribute}=", new_value)
  self
end
destroy_key!(attribute, key) click to toggle source

Deletes a key in a record. E.g.

witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_key(:info, :father)

It does save the record.

# File lib/activerecord-postgres-hstore/activerecord.rb, line 42
def destroy_key! attribute, key
  destroy_key(attribute, key).save
end
destroy_keys(attribute, *keys) click to toggle source

Deletes many keys in a record. E.g.

witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_keys(:info, :father, :mother)

It does not save the record, so you’ll have to do it.

# File lib/activerecord-postgres-hstore/activerecord.rb, line 50
def destroy_keys attribute, *keys
  for key in keys
    new_value = send(attribute)
    new_value.delete(key.to_s)
    send("#{attribute}=", new_value)
  end
  self
end
destroy_keys!(attribute, *keys) click to toggle source

Deletes many keys in a record. E.g.

witt = Person.find_by_name("Ludwig Wittgenstein")
witt.destroy_keys!(:info, :father, :mother)

It does save the record.

# File lib/activerecord-postgres-hstore/activerecord.rb, line 63
def destroy_keys! attribute, *keys
  raise "invalid attribute #{attribute}" unless self.class.column_names.include?(attribute.to_s)
  destroy_keys(attribute, *keys).save
end