I’m working on a Rails app that existing users can invite additional members to join. The problem with this is that the User model exists in different states and in those different states, different sets of information is required.
For example, John is a member of the site and invites Mary. John enters Mary’s name and email address, a user record is created in the database for Mary, and an invitation email is sent. After she joins, however, the required set of data changes and we require her to enter additional information (e.g. a password).
I’m still learning Ruby on Rails and I don’t see any way to handle this using the standard validation techniques of validates_presence_of, validates_format_of, etc. Can anyone point me in the right direction
The easiest is to use
:ifas follows:or:
You can also use the
validatemethod to define any complex, custom logic. For example:(In the above example, you could actually define the validation rules in
has_name_and_email_after_invitationwith regularvalidates_xxx_ofcalls, because they must apply in step 2 as well, but using two methods for the separate steps gives you maximum flexibility.)