Module: Sequel::Model::DatasetMethods
- Defined in:
- lib/sequel/model/base.rb
Overview
DatasetMethods contains methods that all model datasets have.
Instance Method Summary collapse
- 
  
    
      #[](*args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Assume if a single integer is given that it is a lookup by primary key, and call with_pk with the argument. 
- 
  
    
      #as_hash(key_column = nil, value_column = nil, opts = OPTS)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    This allows you to call as_hashwithout any arguments, which will result in a hash with the primary key value being the key and the model object being the value.
- 
  
    
      #destroy  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Destroy each row in the dataset by instantiating it and then calling destroy on the resulting model object. 
- 
  
    
      #last(*a, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    If there is no order already defined on this dataset, order it by the primary key and call last. 
- 
  
    
      #model  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    The model class associated with this dataset. 
- 
  
    
      #paged_each(*a, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    If there is no order already defined on this dataset, order it by the primary key and call paged_each. 
- 
  
    
      #to_hash(*a)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Alias of as_hash for backwards compatibility. 
- 
  
    
      #with_pk(pk)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Given a primary key value, return the first record in the dataset with that primary key value. 
- 
  
    
      #with_pk!(pk)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Same as with_pk, but raises NoMatchingRow instead of returning nil if no row matches. 
Instance Method Details
#[](*args) ⇒ Object
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
| 2177 2178 2179 2180 2181 2182 2183 | # File 'lib/sequel/model/base.rb', line 2177 def [](*args) if args.length == 1 && (i = args[0]) && i.is_a?(Integer) with_pk(i) else super end end | 
#as_hash(key_column = nil, value_column = nil, opts = OPTS) ⇒ Object
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, ...}>,
#     ...}
| 2237 2238 2239 2240 2241 2242 2243 2244 | # File 'lib/sequel/model/base.rb', line 2237 def as_hash(key_column=nil, value_column=nil, opts=OPTS) if key_column super else raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key) super(pk, value_column, opts) end end | 
#destroy ⇒ Object
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)
# ...
| 2194 2195 2196 2197 2198 | # File 'lib/sequel/model/base.rb', line 2194 def destroy @db.transaction(:server=>opts[:server], :skip_transaction=>model.use_transactions == false) do all(&:destroy).length end end | 
#last(*a, &block) ⇒ Object
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
| 2205 2206 2207 2208 2209 2210 2211 | # File 'lib/sequel/model/base.rb', line 2205 def last(*a, &block) if ds = _primary_key_order ds.last(*a, &block) else super end end | 
#model ⇒ Object
The model class associated with this dataset
Artist.dataset.model # => Artist
| 2169 2170 2171 | # File 'lib/sequel/model/base.rb', line 2169 def model @opts[:model] end | 
#paged_each(*a, &block) ⇒ Object
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
# ...
| 2221 2222 2223 2224 2225 2226 2227 | # File 'lib/sequel/model/base.rb', line 2221 def paged_each(*a, &block) if ds = _primary_key_order ds.paged_each(*a, &block) else super end end | 
#to_hash(*a) ⇒ Object
Alias of as_hash for backwards compatibility.
| 2247 2248 2249 | # File 'lib/sequel/model/base.rb', line 2247 def to_hash(*a) as_hash(*a) end | 
#with_pk(pk) ⇒ Object
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
| 2261 2262 2263 2264 2265 2266 2267 | # File 'lib/sequel/model/base.rb', line 2261 def with_pk(pk) if pk && (loader = _with_pk_loader) loader.first(*pk) else first(model.qualified_primary_key_hash(pk)) end end | 
#with_pk!(pk) ⇒ Object
Same as with_pk, but raises NoMatchingRow instead of returning nil if no row matches.
| 2271 2272 2273 | # File 'lib/sequel/model/base.rb', line 2271 def with_pk!(pk) with_pk(pk) || raise(NoMatchingRow.new(self)) end |