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

Sequel::Model instance methods that implement basic model functionality.

  • All of the methods in HOOKS create instance methods that are called by Sequel when the appropriate action occurs. For example, when destroying a model object, Sequel will call before_destroy, do the destroy, and then call after_destroy.
  • The following instance_methods all call the class method of the same name: columns, dataset, db, primary_key, db_schema.
  • The following instance methods allow boolean flags to be set on a per-object basis: raise_on_save_failure, raise_on_typecast_failure, strict_param_setting, typecast_empty_string_to_nil, typecast_on_assignment, use_transactions. If they are not used, the object will default to whatever the model setting is.

Methods

==   ===   []   []=   associations   autoincrementing_primary_key   changed_columns   delete   destroy   each   eql?   errors   exists?   hash   id   inspect   keys   marshallable!   modified!   modified?   new   new?   pk   pk_hash   refresh   reload   save   save_changes   set   set_all   set_except   set_only   this   update   update_all   update_except   update_only   valid?   validate  

External Aliases

class -> model
  class is defined in Object, but it is also a keyword, and since a lot of instance methods call class methods, this alias makes it so you can use model instead of self.class.

Attributes

values  [R]  The hash of attribute values. Keys are symbols with the names of the underlying database columns.

Public Class methods

Creates new instance and passes the given values to set. If a block is given, yield the instance to the block unless from_db is true. This method runs the after_initialize hook after it has optionally yielded itself to the block.

Arguments:

  • values - should be a hash to pass to set.
  • from_db - should only be set by Model.load, forget it exists.

[Source]

     # File lib/sequel/model/base.rb, line 525
525:       def initialize(values = {}, from_db = false)
526:         if from_db
527:           @new = false
528:           set_values(values)
529:         else
530:           @values = {}
531:           @new = true
532:           @modified = true
533:           set(values)
534:           changed_columns.clear 
535:           yield self if block_given?
536:         end
537:         after_initialize
538:       end

Public Instance methods

Compares model instances by values.

[Source]

     # File lib/sequel/model/base.rb, line 562
562:       def ==(obj)
563:         (obj.class == model) && (obj.values == @values)
564:       end

If pk is not nil, true only if the objects have the same class and pk. If pk is nil, false.

[Source]

     # File lib/sequel/model/base.rb, line 569
569:       def ===(obj)
570:         pk.nil? ? false : (obj.class == model) && (obj.pk == pk)
571:       end

Returns value of the column‘s attribute.

[Source]

     # File lib/sequel/model/base.rb, line 541
541:       def [](column)
542:         @values[column]
543:       end

Sets the value for the given column. If typecasting is enabled for this object, typecast the value based on the column‘s type. If this a a new record or the typecasted value isn‘t the same as the current value for the column, mark the column as changed.

[Source]

     # File lib/sequel/model/base.rb, line 549
549:       def []=(column, value)
550:         # If it is new, it doesn't have a value yet, so we should
551:         # definitely set the new value.
552:         # If the column isn't in @values, we can't assume it is
553:         # NULL in the database, so assume it has changed.
554:         v = typecast_value(column, value)
555:         if new? || !@values.include?(column) || v != @values[column]
556:           changed_columns << column unless changed_columns.include?(column)
557:           @values[column] = v
558:         end
559:       end

The current cached associations. A hash with the keys being the association name symbols and the values being the associated object or nil (many_to_one), or the array of associated objects (*_to_many).

[Source]

     # File lib/sequel/model/base.rb, line 582
582:       def associations
583:         @associations ||= {}
584:       end

The autoincrementing primary key for this model object. Should be overridden if you have a composite primary key with one part of it being autoincrementing.

[Source]

     # File lib/sequel/model/base.rb, line 589
589:       def autoincrementing_primary_key
590:         primary_key
591:       end

The columns that have been updated. This isn‘t completely accurate, see Model#[]=.

[Source]

     # File lib/sequel/model/base.rb, line 595
595:       def changed_columns
596:         @changed_columns ||= []
597:       end

Deletes and returns self. Does not run destroy hooks. Look into using destroy instead.

[Source]

     # File lib/sequel/model/base.rb, line 601
601:       def delete
602:         this.delete
603:         self
604:       end

Like delete but runs hooks before and after delete. If before_destroy returns false, returns false without deleting the object the the database. Otherwise, deletes the item from the database and returns self. Uses a transaction if use_transactions is true.

[Source]

     # File lib/sequel/model/base.rb, line 611
611:       def destroy
612:         use_transactions ? db.transaction{_destroy} : _destroy
613:       end

Iterates through all of the current values using each.

Example:

  Ticket.find(7).each { |k, v| puts "#{k} => #{v}" }

[Source]

     # File lib/sequel/model/base.rb, line 619
619:       def each(&block)
620:         @values.each(&block)
621:       end
eql?(obj)

Alias for #==

Returns the validation errors associated with this object.

[Source]

     # File lib/sequel/model/base.rb, line 624
624:       def errors
625:         @errors ||= Errors.new
626:       end

Returns true when current instance exists, false otherwise. Generally an object that isn‘t new will exist unless it has been deleted.

[Source]

     # File lib/sequel/model/base.rb, line 631
631:       def exists?
632:         this.count > 0
633:       end

Value that should be unique for objects with the same class and pk (if pk is not nil), or the same class and values (if pk is nil).

[Source]

     # File lib/sequel/model/base.rb, line 637
637:       def hash
638:         [model, pk.nil? ? @values.sort_by{|k,v| k.to_s} : pk].hash
639:       end

Returns value for the :id attribute, even if the primary key is not id. To get the primary key value, use pk.

[Source]

     # File lib/sequel/model/base.rb, line 643
643:       def id
644:         @values[:id]
645:       end

Returns a string representation of the model instance including the class name and values.

[Source]

     # File lib/sequel/model/base.rb, line 649
649:       def inspect
650:         "#<#{model.name} @values=#{inspect_values}>"
651:       end

Returns the keys in values. May not include all column names.

[Source]

     # File lib/sequel/model/base.rb, line 654
654:       def keys
655:         @values.keys
656:       end

Remove elements of the model object that make marshalling fail. Returns self.

[Source]

     # File lib/sequel/model/base.rb, line 659
659:       def marshallable!
660:         @this = nil
661:         self
662:       end

Explicitly mark the object as modified, so save_changes/update will run callbacks even if no columns have changed.

[Source]

     # File lib/sequel/model/base.rb, line 666
666:       def modified!
667:         @modified = true
668:       end

Whether this object has been modified since last saved, used by save_changes to determine whether changes should be saved. New values are always considered modified.

[Source]

     # File lib/sequel/model/base.rb, line 673
673:       def modified?
674:         @modified || !changed_columns.empty?
675:       end

Returns true if the current instance represents a new record.

[Source]

     # File lib/sequel/model/base.rb, line 678
678:       def new?
679:         @new
680:       end

Returns the primary key value identifying the model instance. Raises an error if this model does not have a primary key. If the model has a composite primary key, returns an array of values.

[Source]

     # File lib/sequel/model/base.rb, line 685
685:       def pk
686:         raise(Error, "No primary key is associated with this model") unless key = primary_key
687:         key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key]
688:       end

Returns a hash identifying the model instance. It should be true that:

 Model[model_instance.pk_hash] === model_instance

[Source]

     # File lib/sequel/model/base.rb, line 693
693:       def pk_hash
694:         model.primary_key_hash(pk)
695:       end

Reloads attributes from database and returns self. Also clears all cached association and changed_columns information. Raises an Error if the record no longer exists in the database.

[Source]

     # File lib/sequel/model/base.rb, line 700
700:       def refresh
701:         _refresh(this)
702:       end

Alias of refresh, but not aliased directly to make overriding in a plugin easier.

[Source]

     # File lib/sequel/model/base.rb, line 705
705:       def reload
706:         refresh
707:       end

Creates or updates the record, after making sure the record is valid. If the record is not valid, or before_save, before_create (if new?), or before_update (if !new?) return false, returns nil unless raise_on_save_failure is true (if it is true, it raises an error). Otherwise, returns self. You can provide an optional list of columns to update, in which case it only updates those columns.

Takes the following options:

  • :changed - save all changed columns, instead of all columns or the columns given
  • :transaction - set to false not to use a transaction
  • :validate - set to false not to validate the model before saving

[Source]

     # File lib/sequel/model/base.rb, line 722
722:       def save(*columns)
723:         opts = columns.last.is_a?(Hash) ? columns.pop : {}
724:         return save_failure(:invalid) if opts[:validate] != false and !valid?
725:         use_transaction = opts.include?(:transaction) ? opts[:transaction] : use_transactions
726:         use_transaction ? db.transaction(opts){_save(columns, opts)} : _save(columns, opts)
727:       end

Saves only changed columns if the object has been modified. If the object has not been modified, returns nil. If unable to save, returns false unless raise_on_save_failure is true.

[Source]

     # File lib/sequel/model/base.rb, line 732
732:       def save_changes(opts={})
733:         save(opts.merge(:changed=>true)) || false if modified? 
734:       end

Updates the instance with the supplied values with support for virtual attributes, raising an exception if a value is used that doesn‘t have a setter method (or ignoring it if strict_param_setting = false). Does not save the record.

[Source]

     # File lib/sequel/model/base.rb, line 740
740:       def set(hash)
741:         set_restricted(hash, nil, nil)
742:       end

Set all values using the entries in the hash, ignoring any setting of allowed_columns or restricted columns in the model.

[Source]

     # File lib/sequel/model/base.rb, line 746
746:       def set_all(hash)
747:         set_restricted(hash, false, false)
748:       end

Set all values using the entries in the hash, except for the keys given in except.

[Source]

     # File lib/sequel/model/base.rb, line 752
752:       def set_except(hash, *except)
753:         set_restricted(hash, false, except.flatten)
754:       end

Set the values using the entries in the hash, only if the key is included in only.

[Source]

     # File lib/sequel/model/base.rb, line 758
758:       def set_only(hash, *only)
759:         set_restricted(hash, only.flatten, false)
760:       end

Returns (naked) dataset that should return only this instance.

[Source]

     # File lib/sequel/model/base.rb, line 763
763:       def this
764:         @this ||= model.dataset.filter(pk_hash).limit(1).naked
765:       end

Runs set with the passed hash and then runs save_changes.

[Source]

     # File lib/sequel/model/base.rb, line 768
768:       def update(hash)
769:         update_restricted(hash, nil, nil)
770:       end

Update all values using the entries in the hash, ignoring any setting of allowed_columns or restricted columns in the model.

[Source]

     # File lib/sequel/model/base.rb, line 774
774:       def update_all(hash)
775:         update_restricted(hash, false, false)
776:       end

Update all values using the entries in the hash, except for the keys given in except.

[Source]

     # File lib/sequel/model/base.rb, line 780
780:       def update_except(hash, *except)
781:         update_restricted(hash, false, except.flatten)
782:       end

Update the values using the entries in the hash, only if the key is included in only.

[Source]

     # File lib/sequel/model/base.rb, line 786
786:       def update_only(hash, *only)
787:         update_restricted(hash, only.flatten, false)
788:       end

Validates the object and returns true if no errors are reported.

[Source]

     # File lib/sequel/model/base.rb, line 797
797:       def valid?
798:         errors.clear
799:         if before_validation == false
800:           save_failure(:validation)
801:           return false
802:         end
803:         validate
804:         after_validation
805:         errors.empty?
806:       end

Validates the object. If the object is invalid, errors should be added to the errors attribute. By default, does nothing, as all models are valid by default.

[Source]

     # File lib/sequel/model/base.rb, line 793
793:       def validate
794:       end

[Validate]