module Sequel::Model::DatasetMethods

  1. lib/sequel/model/base.rb

DatasetMethods contains methods that all model datasets have.

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
[show source]
     # File lib/sequel/model/base.rb
2138 def [](*args)
2139   if args.length == 1 && (i = args[0]) && i.is_a?(Integer)
2140     with_pk(i)
2141   else
2142     super
2143   end
2144 end
as_hash(key_column=nil, value_column=nil, opts=OPTS)

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, ...}>,
#     ...}
[show source]
     # File lib/sequel/model/base.rb
2198 def as_hash(key_column=nil, value_column=nil, opts=OPTS)
2199   if key_column
2200     super
2201   else
2202     raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key)
2203     super(pk, value_column, opts) 
2204   end
2205 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)
# ...
[show source]
     # File lib/sequel/model/base.rb
2155 def destroy
2156   @db.transaction(:server=>opts[:server], :skip_transaction=>model.use_transactions == false) do
2157     all(&:destroy).length
2158   end
2159 end
last(*a, &block)

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
[show source]
     # File lib/sequel/model/base.rb
2166 def last(*a, &block)
2167   if ds = _primary_key_order
2168     ds.last(*a, &block)
2169   else
2170     super
2171   end
2172 end
model()

The model class associated with this dataset

Artist.dataset.model # => Artist
[show source]
     # File lib/sequel/model/base.rb
2130 def model
2131   @opts[:model]
2132 end
paged_each(*a, &block)

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
# ...
[show source]
     # File lib/sequel/model/base.rb
2182 def paged_each(*a, &block)
2183   if ds = _primary_key_order
2184     ds.paged_each(*a, &block)
2185   else
2186     super
2187   end
2188 end
to_hash(*a)

Alias of as_hash for backwards compatibility.

[show source]
     # File lib/sequel/model/base.rb
2208 def to_hash(*a)
2209   as_hash(*a)
2210 end
with_pk(pk)

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
[show source]
     # File lib/sequel/model/base.rb
2222 def with_pk(pk)
2223   if pk && (loader = _with_pk_loader)
2224     loader.first(*pk)
2225   else
2226     first(model.qualified_primary_key_hash(pk))
2227   end
2228 end
with_pk!(pk)

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

[show source]
     # File lib/sequel/model/base.rb
2232 def with_pk!(pk)
2233   with_pk(pk) || raise(NoMatchingRow.new(self))
2234 end