Module: DatabaseValidations::BelongsToHandlers

Extended by:
ActiveSupport::Concern
Defined in:
lib/database_validations/validations/belongs_to_handlers.rb

Instance Method Summary collapse

Instance Method Details

#save(opts = {}) ⇒ Object



29
30
31
32
33
34
# File 'lib/database_validations/validations/belongs_to_handlers.rb', line 29

def save(opts = {})
  ActiveRecord::Base.connection.transaction(requires_new: true) { super }
rescue ActiveRecord::InvalidForeignKey => e
  Helpers.handle_foreign_key_error!(self, e)
  false
end

#save!(opts = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/database_validations/validations/belongs_to_handlers.rb', line 36

def save!(opts = {})
  ActiveRecord::Base.connection.transaction(requires_new: true) { super }
rescue ActiveRecord::InvalidForeignKey => e
  Helpers.handle_foreign_key_error!(self, e)
  raise ActiveRecord::RecordInvalid, self
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/database_validations/validations/belongs_to_handlers.rb', line 17

def valid?(context = nil)
  output = super(context)

  Helpers.each_belongs_to_presence_validator(self.class) do |validator|
    next if validator.column_and_relation_blank_for?(self)

    validates_with(ActiveRecord::Validations::PresenceValidator, validator.validates_presence_options)
  end

  errors.empty? && output
end