Waterlink
Waterlink

Reputation: 2369

Rails running validations on custom model manually

We have custom model. It is working without database and includes some mixins from active record:

class Node
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :title, :content

  validates_presence_of :title, :content

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

  def save
    # we want to run validations here
  end
end

Through googling got that it is possible to use @object.validate, but it complains about not having such method.

Help, please.

Upvotes: 3

Views: 8223

Answers (1)

Matenia Rossides
Matenia Rossides

Reputation: 1414

You are correct, .validate seems to be undefined.

@object.valid? should do the job for what you want.

Keep in mind, this returns a boolean value which you can use to control conditional behaviour based on your requirements.

Upvotes: 8

Related Questions