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

Dataset methods are methods that the model class extends its dataset with in the call to set_dataset.

Methods

[]   destroy   graph   insert_sql   join_table   last   paged_each   to_hash   with_pk   with_pk!  

Attributes

model  [RW]  The model class associated with this dataset
  Artist.dataset.model # => Artist

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 2380
2380:       def [](*args)
2381:         if args.length == 1 && (i = args.at(0)) && i.is_a?(Integer)
2382:           with_pk(i)
2383:         else
2384:           super
2385:         end
2386:       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 2397
2397:       def destroy
2398:         pr = proc{all(&:destroy).length}
2399:         model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call
2400:       end

Allow Sequel::Model classes to be used as dataset arguments when graphing:

  Artist.graph(Album, :artist_id=>id)
  # SELECT artists.id, artists.name, albums.id AS albums_id, albums.artist_id, albums.name AS albums_name
  # FROM artists LEFT OUTER JOIN albums ON (albums.artist_id = artists.id)

[Source]

      # File lib/sequel/model/base.rb, line 2407
2407:       def graph(table, *args, &block)
2408:         if table.is_a?(Class) && table < Sequel::Model
2409:           super(table.dataset, *args, &block)
2410:         else
2411:           super
2412:         end
2413:       end

Handle Sequel::Model instances when inserting, using the model instance‘s values for the insert, unless the model instance can be used directly in SQL.

  Album.insert(Album.load(:name=>'A'))
  # INSERT INTO albums (name) VALUES ('A')

[Source]

      # File lib/sequel/model/base.rb, line 2421
2421:       def insert_sql(*values)
2422:         if values.size == 1 && (v = values.at(0)).is_a?(Sequel::Model) && !v.respond_to?(:sql_literal_append)
2423:           super(v.to_hash)
2424:         else
2425:           super
2426:         end
2427:       end

Allow Sequel::Model classes to be used as table name arguments in dataset join methods:

  Artist.join(Album, :artist_id=>id)
  # SELECT * FROM artists INNER JOIN albums ON (albums.artist_id = artists.id)

[Source]

      # File lib/sequel/model/base.rb, line 2434
2434:       def join_table(type, table, *args, &block)
2435:         if table.is_a?(Class) && table < Sequel::Model
2436:           if table.dataset.simple_select_all?
2437:             super(type, table.table_name, *args, &block)
2438:           else
2439:             super(type, table.dataset, *args, &block)
2440:           end
2441:         else
2442:           super
2443:         end
2444:       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 2451
2451:       def last(*a, &block)
2452:         if opts[:order].nil? && model && (pk = model.primary_key)
2453:           order(*pk).last(*a, &block)
2454:         else
2455:           super
2456:         end
2457:       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 2467
2467:       def paged_each(*a, &block)
2468:         if opts[:order].nil? && model && (pk = model.primary_key)
2469:           order(*pk).paged_each(*a, &block)
2470:         else
2471:           super
2472:         end
2473:       end

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

[Source]

      # File lib/sequel/model/base.rb, line 2483
2483:       def to_hash(key_column=nil, value_column=nil, opts=OPTS)
2484:         if key_column
2485:           super
2486:         else
2487:           raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key)
2488:           super(pk, value_column, opts) 
2489:         end
2490:       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 2502
2502:       def with_pk(pk)
2503:         first(model.qualified_primary_key_hash(pk))
2504:       end

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

[Source]

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

[Validate]