Module: Sequel::Model::DatasetMethods

Defined in:
lib/sequel/model/base.rb

Overview

Dataset methods are methods that the model class extends its dataset with in the call to set_dataset.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modelObject

The model class associated with this dataset

Artist.dataset.model # => Artist


1462
1463
1464
# File 'lib/sequel/model/base.rb', line 1462

def model
  @model
end

Instance Method Details

#destroyObject

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 dataset, which does a single SQL call, but this runs any destroy hooks on each object in the dataset.

Artist.dataset.destroy
# DELETE FROM artists WHERE (id = 1)
# DELETE FROM artists WHERE (id = 2)
# ...


1473
1474
1475
1476
# File 'lib/sequel/model/base.rb', line 1473

def destroy
  pr = proc{all{|r| r.destroy}.length}
  model.use_transactions ? @db.transaction(&pr) : pr.call
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.

Artist.dataset.to_hash # SELECT * FROM artists
# => {1=>#<Artist {:id=>1, ...}>,
#     2=>#<Artist {:id=>2, ...}>,
#     ...}


1486
1487
1488
1489
1490
1491
1492
1493
# File 'lib/sequel/model/base.rb', line 1486

def to_hash(key_column=nil, value_column=nil)
  if key_column
    super
  else
    raise(Sequel::Error, "No primary key for model") unless model and pk = model.primary_key
    super(pk, value_column) 
  end
end