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

DatasetMethods contains methods that all model datasets have.

Methods

[]   as_hash   destroy   last   model   paged_each   to_hash   with_pk   with_pk!  

Public Instance methods

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 2026
2026:       def [](*args)
2027:         if args.length == 1 && (i = args[0]) && i.is_a?(Integer)
2028:           with_pk(i)
2029:         else
2030:           super
2031:         end
2032:       end

This allows you to call as_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.as_hash # SELECT * FROM artists
  # => {1=>#<Artist {:id=>1, ...}>,
  #     2=>#<Artist {:id=>2, ...}>,
  #     ...}

[Source]

      # File lib/sequel/model/base.rb, line 2085
2085:       def as_hash(key_column=nil, value_column=nil, opts=OPTS)
2086:         if key_column
2087:           super
2088:         else
2089:           raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key)
2090:           super(pk, value_column, opts) 
2091:         end
2092:       end

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 2043
2043:       def destroy
2044:         pr = proc{all(&:destroy).length}
2045:         model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call
2046:       end

If there is no order already defined on this dataset, order it by the primary key and call last.

  Album.last
  # SELECT * FROM albums ORDER BY id DESC LIMIT 1

[Source]

      # File lib/sequel/model/base.rb, line 2053
2053:       def last(*a, &block)
2054:         if ds = _primary_key_order
2055:           ds.last(*a, &block)
2056:         else
2057:           super
2058:         end
2059:       end

The model class associated with this dataset

  Artist.dataset.model # => Artist

[Source]

      # File lib/sequel/model/base.rb, line 2018
2018:       def model
2019:         @opts[:model]
2020:       end

If there is no order already defined on this dataset, order it by the primary key and call paged_each.

  Album.paged_each{|row| }
  # SELECT * FROM albums ORDER BY id LIMIT 1000 OFFSET 0
  # SELECT * FROM albums ORDER BY id LIMIT 1000 OFFSET 1000
  # SELECT * FROM albums ORDER BY id LIMIT 1000 OFFSET 2000
  # ...

[Source]

      # File lib/sequel/model/base.rb, line 2069
2069:       def paged_each(*a, &block)
2070:         if ds = _primary_key_order
2071:           ds.paged_each(*a, &block)
2072:         else
2073:           super
2074:         end
2075:       end

Alias of as_hash for backwards compatibility.

[Source]

      # File lib/sequel/model/base.rb, line 2095
2095:       def to_hash(*a)
2096:         as_hash(*a)
2097:       end

Given a primary key value, return the first record in the dataset with that primary key value. If no records matches, returns nil.

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

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

[Source]

      # File lib/sequel/model/base.rb, line 2109
2109:       def with_pk(pk)
2110:         if pk && (loader = _with_pk_loader)
2111:           loader.first(*pk)
2112:         else
2113:           first(model.qualified_primary_key_hash(pk))
2114:         end
2115:       end

Same as with_pk, but raises NoMatchingRow instead of returning nil if no row matches.

[Source]

      # File lib/sequel/model/base.rb, line 2119
2119:       def with_pk!(pk)
2120:         with_pk(pk) || raise(NoMatchingRow.new(self))
2121:       end

[Validate]