Ask A Question

Notifications

You’re not receiving notifications from this thread.

User Referral Program From Scratch Discussion

Just the thing I was needed for a current project, as usual, perfect timing Chris !

Reply

I love when that happens!

Reply

TIP: If any of you are using validations in devise, before_create in the user.rb file may not work, and you will get an error.

Therefore, try changing that line in user.rb to before_validation and you may find that it works.

Reply

Quick note here: you'll want to add a conditional check if self.referral_code.nil? to the make sure you don't re-generate and overwrite the existing referral code in the event a user needs to use the Forgot Password function.

Reply

You're correct @Monroe...I was getting an error with before_create claiming that the referral code already existed

Reply

Hi Charlie, so if someone clicks 'forgot password', that's going to have an effect on the referral code? Can you elaborate?

Hi Taylor, glad I was able to help!

Reply

hey guys i changed the before create to before validate and it works but think maybe the code will not generate a unique one every time and retry until it does?

Could someone explain the step by step on how it validates the codes uniqueness?

cheers

Reply

My understanding is that the method set_refferal_code that generates the code in the first place is ensuring that the code is unique. It will keep generating new codes, until it makes a unique one. I also found this article pretty helpful to learn about before and after methods. https://guides.rubyonrails.org/active_record_callbacks.html

Reply

I think you would actually want to call before_validation :set_referral_code, on: :create to summarize the comments from above.

Reply

Nice one! Want to expand on this one, but a great start - thanks.

Reply

I tried using it with omniauth : facebook and google it doesn't save the referred_by_id, any solution ?

Reply

When I try to create the new app using the jumpstart template, I land on this error

insert  config/webpack/environment.js
The file /Users/ijsemerene/Documents/GitHub/jumpstart/referral_app/config/webpack/environment.js does not appear to exist

Any ideas on how to address it?

Reply

Hey Ignacio,

If you look through the logs above, did it fail when installing webpacker?

Reply

Yup, looks like it's failing there first

Reply

What's the error?

Reply

Fantastic! glad I found this! was looking at different options and as always, cheaper to roll your own lol..!

Thanks again Chris!

Reply

Hi Chris! Any suggestion on how to proceed with the complete_reffral! method. I would like to give the referrer a free post without him having to go through stripe checkout for payment.

Reply

You can just call the complete_referral! method anytime the user completes the steps you want. If you want to do that right after registration, go add that after sign up. 👍

Reply

Any ideas on how one would go about adding this to a stripe checkout flow? I would like the referree to have a free post instead of going through the checkout if he has a referred user who has the referral_completed_at? as true.

Reply

Hi Chris! This referral program assumes that an user can only be referred once, right? What if two users reffer the same person? Thanks!

Reply

Only one referrer will get credit, typically that's the last referrer. That's how basically all referral programs work.

Reply

Great video Chris - I found it especially useful how you went into detail on how to modify the Devise controller method. Also nice seeing the decision process behind override the whole function or just the build resource.

Reply

Great video! Thank you for it. I have one question how can you call the complete for example after a plan subscription from a user. Thanks

Reply

Thanks for the video Chris. Would you do this in a different way if there were multiple levels to the referral.
For example:
A refers B
B accepts
B refers C
C accepts

A gets rewarded for both, B accepting the referral and C accepting the referral. For multi level referral, the rewards are lower. Do you think the same approach will be efficient in this case?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.