I have a user model, that stores, amongst other things, the users address. When creating an account (user_sessions controller) I only wish to validate the presence of their name, the address is optional.
When they upgrade their account (upgrade controller) i want to validate that that their full address is entered.
Is there an elegant way of having a conditional validation based on the controller?
Have you tried creating a new model that inherits from the old one. So you could have:
Since it inherits from the
Userclass, theUpgradedUserclass has validations for both address and name. And this solution would still be only using one database table. Because they are still one table, you’d have to add a:account_statuscolumn or something to determine whether an account has been upgraded or not.In your upgraded_users controller you would have a standard update method:
This method will access the same User table just like the
users_controllerwould, but its simply doing so through theUpgradedUsermodel instead, and therefore would check the validators on save for both name and address presence.