DatasetMethods
contains methods that all model datasets have.
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
# File lib/sequel/model/base.rb 2121 def [](*args) 2122 if args.length == 1 && (i = args[0]) && i.is_a?(Integer) 2123 with_pk(i) 2124 else 2125 super 2126 end 2127 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, ...}>, # ...}
# File lib/sequel/model/base.rb 2180 def as_hash(key_column=nil, value_column=nil, opts=OPTS) 2181 if key_column 2182 super 2183 else 2184 raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key) 2185 super(pk, value_column, opts) 2186 end 2187 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) # ...
# File lib/sequel/model/base.rb 2138 def destroy 2139 pr = proc{all(&:destroy).length} 2140 model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call 2141 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
# File lib/sequel/model/base.rb 2148 def last(*a, &block) 2149 if ds = _primary_key_order 2150 ds.last(*a, &block) 2151 else 2152 super 2153 end 2154 end
The model class associated with this dataset
Artist.dataset.model # => Artist
# File lib/sequel/model/base.rb 2113 def model 2114 @opts[:model] 2115 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 # ...
# File lib/sequel/model/base.rb 2164 def paged_each(*a, &block) 2165 if ds = _primary_key_order 2166 ds.paged_each(*a, &block) 2167 else 2168 super 2169 end 2170 end
Alias of as_hash
for backwards compatibility.
# File lib/sequel/model/base.rb 2190 def to_hash(*a) 2191 as_hash(*a) 2192 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
# File lib/sequel/model/base.rb 2204 def with_pk(pk) 2205 if pk && (loader = _with_pk_loader) 2206 loader.first(*pk) 2207 else 2208 first(model.qualified_primary_key_hash(pk)) 2209 end 2210 end
Same as with_pk
, but raises NoMatchingRow
instead of returning nil if no row matches.
# File lib/sequel/model/base.rb 2214 def with_pk!(pk) 2215 with_pk(pk) || raise(NoMatchingRow.new(self)) 2216 end