Module | Sequel::Model::DatasetMethods
In: | lib/sequel/model/base.rb

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

### Methods

[] destroy to_hash with_pk

### Attributes

model

[RW]

The model class associated with this dataset

      Artist.dataset.model # => Artist

### Public Instance methods

[](*args)

Assume if a single integer is given that it is a lookup by primary key, and call with_pk with the argument.

      Artist.dataset[1] # SELECT * FROM artists WHERE (id = 1) LIMIT 1

[Source]

          # File lib/sequel/model/base.rb, line 1852
    1852:       def [](*args)
    1853:         if args.length == 1 && (i = args.at(0)) && i.is_a?(Integer)
    1854:           with_pk(i)
    1855:         else
    1856:           super
    1857:         end
    1858:       end

destroy()

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)
      # ...

[Source]

          # File lib/sequel/model/base.rb, line 1869
    1869:       def destroy
    1870:         pr = proc{all{|r| r.destroy}.length}
    1871:         model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call
    1872:       end

to_hash(key_column=nil, value_column=nil)

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, ...}>,
      #     ...}

[Source]

          # File lib/sequel/model/base.rb, line 1882
    1882:       def to_hash(key_column=nil, value_column=nil)
    1883:         if key_column
    1884:           super
    1885:         else
    1886:           raise(Sequel::Error, "No primary key for model") unless model and pk = model.primary_key
    1887:           super(pk, value_column)
    1888:         end
    1889:       end

with_pk(pk)

Given a primary key value, return the first record in the dataset with that primary key value.

      # Single primary key
      Artist.dataset.with_pk(1) # SELECT * FROM artists WHERE (id = 1) LIMIT 1

      # Composite primary key
      Artist.dataset.with_pk([1, 2]) # SELECT * FROM artists
                                     # WHERE ((id1 = 1) AND (id2 = 2)) LIMIT 1

[Source]

          # File lib/sequel/model/base.rb, line 1900
    1900:       def with_pk(pk)
    1901:         first(model.qualified_primary_key_hash(pk))
    1902:       end

[Validate]