Léo Coletta
Léo Coletta

Reputation: 1279

Rails custom validation method : "ArgumentError (You need to supply at least one validation)"

I try to make a custom validation for a comment method which will verify that if a comment id is present then it must exists.

class Comment < ActiveRecord::Base
  belongs_to :article
  #Many to one relation with itself (1 comment can have one parent and multiple child)
  has_many :child, class_name: "Comment", foreign_key: "parent_id"
  belongs_to :parent, class_name: "Comment", optional: true

  belongs_to :user, optional: true

  validates :comment, presence: true, length: { minimum: 10 }
  validates :first_name, presence: true, length: { minimum: 2, maximum: 30 }
  validates :last_name, length: { minimum: 2, maximum: 30 }
  validates :comment_id_presence

  private

  def comment_id_presence
    if comment_id != nil && !Comment.exists?(comment_id)
      errors.add(:comment_id, "Le parent doit exister ou être nulle")
    end
  end
end

However, I get the following error : ArgumentError (You need to supply at least one validation). I'd like to know what is my mistake or if there is simplier way to do what I want.

Upvotes: 0

Views: 1332

Answers (1)

user3309314
user3309314

Reputation: 2543

You should use just validate without s on the end for this case:

validate :comment_id_presence

More here

Upvotes: 2

Related Questions