validating a field from another table in a specific model

-1

Is it possible to validate the field of another table that is related in a specific model?

For example, I need to validate an email field in a Orders table, but only emails from an internal user (which is in a Users table) . That is, who is an internal user should enter his email. This information comes from login (devise), that is, from another table (requests belongs_to user).

In this, in the requests model, I'm creating a validation def like this:

def valida_email_interno
   errors.add(:email, "Digite o email") if email.blank? and ...# a  condição que o usuario é interno, por exemplo, @pedido.user.localpedido == 1

Any ideas?

    
asked by anonymous 16.03.2015 / 15:28

1 answer

0

Firstly I suggest that you create a method to check if it is an internal user (so I understood only to check if it is associated with a user), for example:

def usuario_interno?
  user # Ou qualquer outra lógica
end

After that you can do a normal rails validation:

validates_presence_of :email, if: :usuario_interno?
    
17.03.2015 / 23:38