← Back to Upcase

ActiveRecord Validate presence help

(Scott Hollinshead) #1

I have a form that I am using for a help desk.

The form is in 2 halfs, the first half is where a user will submit there probelm.

The second half is what the Maintenance team will fill in to action the request. I have a maint_admin field in the db to choose who sees that half of the form, but becasue I have validates presence: true in the model, when a normal user submits the form (who can’t see the other half) it fails validation as they can’t see the form to put anything in the fields.

How could I get around this problem?


(Lenart Rudel) #2

I think you’re looking for something like validate :some_admin_field, presence: true, if: -> record { record.should_validate_admin_fields? }. It’s up to you then to define when the validations should happen. You might need something like a state which you change once the ticket gets passed to admin or you might want to implement some more complex logic.

(Sean Griffin) #3

You should provide a default value that is false instead of nil. You can use the method change_column_default to do this from a migration.

(Scott Hollinshead) #4

@seangriffin I cannot seem to do that as some of the fields are TEXT, so it is not letting me change the default value to False