I was building a rails web, and used form object to separate the validation logics per form.
I had to find_referral_code twice in order to store the user_id of referrer. I wonder how I can improve it.
after_sign_up_form.rb
def get_referred_by_id
User.where(referral_code: referer_referral_code).first.id
end
def validates_presence_of_referer_referral_code
u = User.find_by_referral_code(referer_referral_code)
if u.nil?
errors.add :referer_referral_code, "Not a valid Referral Code"
else
if @user.referral_code == referer_referral_code
errors.add :referer_referral_code, "Can't use your own referral code"
end
end
end
def validates_unique_primary_email
# errors.add :primary_email, "Email is taken"
end
def email_is_self
(@user.identities.pluck(:email) << @user.email).flatten.include? primary_email
end