Checks the association in the child class of the model

I have to deal with an unusual problem when checking for an association in a derived class.

Class Contact < ActiveRecord::Base has_and_belongs_to_many :things end Class SubContact < Contact # validates_presence_of :things validate :must_have_things def must_have_things if things.blank? errors.add("Must select things") end end end 

In the SubContact class SubContact I try to check for things, but nothing works. I have tried both custom and built-in validators. How can I achieve this?

+5
source share
1 answer

add need an attribute. Try using:

 errors.add(:base, "Must not be blank") 

In this case, it is a base class, but it can be any other attribute. Specify the attribute name of the SubContract or :base class.

+3
source

Source: https://habr.com/ru/post/1237721/


All Articles