Module: Sequel::Model::DatasetMethods
- Defined in:
- lib/sequel_model/dataset_methods.rb
Overview
Dataset methods are methods that the model class extends its dataset with in the call to set_dataset.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroy each row in the dataset by instantiating it and then calling destroy on the resulting model object.
-
#to_hash(key_column = nil, value_column = nil) ⇒ Object
This allows you to call to_hash without any arguments, which will result in a hash with the primary key value being the key and the model object being the value.
Instance Method Details
#destroy ⇒ Object
Destroy each row in the dataset by instantiating it and then calling destroy on the resulting model object. This isn’t as fast as deleting the object, which does a single SQL call, but this runs any destroy hooks.
8 9 10 11 12 13 |
# File 'lib/sequel_model/dataset_methods.rb', line 8 def destroy raise(Error, "No model associated with this dataset") unless @opts[:models] count = 0 @db.transaction {each {|r| count += 1; r.destroy}} count end |
#to_hash(key_column = nil, value_column = nil) ⇒ Object
This allows you to call to_hash without any arguments, which will result in a hash with the primary key value being the key and the model object being the value.
18 19 20 21 22 23 24 25 |
# File 'lib/sequel_model/dataset_methods.rb', line 18 def to_hash(key_column=nil, value_column=nil) if key_column super else raise(Sequel::Error, "No primary key for model") unless pk = @opts[:models][nil].primary_key super(pk, value_column) end end |