lundi 16 mai 2016

Moving a validation into a before_create callback

I have an invitation system where the user must have an invitation with a signed token in order to register on the site. This token is received by email from either a current user or an admin.

I have a validation on the User.rb model: validates_uniqueness_of :invitation_id preventing any new user from using a token that wasn't sent to them directly.

I want to move this into a before_create callback so that I can better style and customize the error message through the flash, like so:

Users Model

before_create :invitation_id_is_unique
...
def invitation_id_is_unique
  unless User.invitation.find_by_id(params[:id])
  true
end

Yet this isn't working. It has to be my find_by logic, but I can't figure it out. Any help?

Aucun commentaire:

Enregistrer un commentaire