Module: Sequel::Plugins::ValidateAssociated

Defined in:
lib/sequel/plugins/validate_associated.rb

Overview

The validate_associated plugin allows you to validate associated objects. It also offers the ability to delay the validation of associated objects until the current object is validated. If the associated object is invalid, validation error messages from the associated object will be added to the current object’s validation errors.

Usage:

# Make all model subclass support validating associated objects
Sequel::Model.plugin :validate_associated

# Make the Album class support validating associated objects
Album.plugin :validate_associated

class Album
  many_to_one :artist
  many_to_many :tags

  # Always validate associated artist when saving the album
  def validate
    super
    if artist
      validate_associated_object(model.association_reflection(:artist), artist)
    end
  end

  # When saving after calling this method, validate the given tag as well.
  def check_tag!(tag)
    delay_validate_associated_object(model.association_reflection(:tags), tag)
  end
end

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.apply(mod) ⇒ Object

Depend on the instance_hooks plugin.



39
40
41
# File 'lib/sequel/plugins/validate_associated.rb', line 39

def self.apply(mod)
  mod.plugin :instance_hooks
end