Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/activerecord-postgres-hstore/activerecord.rb
Overview
Adds methods for deleting keys in your hstore columns
Class Method Summary collapse
-
.delete_key(attribute, key) ⇒ Object
Deletes all keys from a specific column in a model.
-
.delete_keys(attribute, *keys) ⇒ Object
Deletes many keys from a specific column in a model.
Instance Method Summary collapse
-
#destroy_key(attribute, key) ⇒ Object
Deletes a key in a record.
-
#destroy_key!(attribute, key) ⇒ Object
Deletes a key in a record.
-
#destroy_keys(attribute, *keys) ⇒ Object
Deletes many keys in a record.
-
#destroy_keys!(attribute, *keys) ⇒ Object
Deletes many keys in a record.
Class Method Details
.delete_key(attribute, key) ⇒ Object
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');
10 11 12 13 |
# 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) ⇒ Object
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');
19 20 21 22 23 24 |
# 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 |
Instance Method Details
#destroy_key(attribute, key) ⇒ Object
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.
30 31 32 33 34 35 36 |
# 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) ⇒ Object
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.
42 43 44 |
# File 'lib/activerecord-postgres-hstore/activerecord.rb', line 42 def destroy_key! attribute, key destroy_key(attribute, key).save end |
#destroy_keys(attribute, *keys) ⇒ Object
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.
50 51 52 53 54 55 56 57 |
# 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) ⇒ Object
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.
63 64 65 66 |
# 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 |