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 2177 def [](*args) 2178 if args.length == 1 && (i = args[0]) && i.is_a?(Integer) 2179 with_pk(i) 2180 else 2181 super 2182 end 2183 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 2237 def as_hash(key_column=nil, value_column=nil, opts=OPTS) 2238 if key_column 2239 super 2240 else 2241 raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key) 2242 super(pk, value_column, opts) 2243 end 2244 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 2194 def destroy 2195 @db.transaction(:server=>opts[:server], :skip_transaction=>model.use_transactions == false) do 2196 all(&:destroy).length 2197 end 2198 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 2205 def last(*a, &block) 2206 if ds = _primary_key_order 2207 ds.last(*a, &block) 2208 else 2209 super 2210 end 2211 end
The model class associated with this dataset
Artist.dataset.model # => Artist
# File lib/sequel/model/base.rb 2169 def model 2170 @opts[:model] 2171 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 2221 def paged_each(*a, &block) 2222 if ds = _primary_key_order 2223 ds.paged_each(*a, &block) 2224 else 2225 super 2226 end 2227 end
Alias of as_hash
for backwards compatibility.
# File lib/sequel/model/base.rb 2247 def to_hash(*a) 2248 as_hash(*a) 2249 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 2261 def with_pk(pk) 2262 if pk && (loader = _with_pk_loader) 2263 loader.first(*pk) 2264 else 2265 first(model.qualified_primary_key_hash(pk)) 2266 end 2267 end
Same as with_pk
, but raises NoMatchingRow
instead of returning nil if no row matches.
# File lib/sequel/model/base.rb 2271 def with_pk!(pk) 2272 with_pk(pk) || raise(NoMatchingRow.new(self)) 2273 end