In this episode, we work on enabling referrals for our users by letting them add their PayPal email address to their profile. We start off by discussing why it's important for the PayPal address to be carefully handled—users should be able to update it, but not delete it entirely once referrals are enabled. This is to prevent payout and referral issues later on.
We dive into the code and start by adding a new field to our user profile form for the PayPal email, making it clear but not required (yet). Then, we update our user model and run a migration to add this new field to the database, ensuring that PayPal emails are unique.
After that, we tweak the validation rules. The new field should be nullable at first, but once a user has enabled referrals (meaning they already have a referral code and PayPal email), they shouldn't be able to remove the PayPal email completely—they can only change it. We achieve this with a custom validation rule (required_if
) and some handy methods in our user model to check if referrals are enabled.
The episode wraps up by making sure everything works: users can set and update their PayPal emails to get access to the referral program, but can't wipe out the field if their referrals are active. Now they're all set up to access the referrals area and start earning rewards!