New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add association between referral and referrer #773
Add association between referral and referrer #773
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want any referral-based columns to exist on the users table, so I think we still need another model. The diagram in the issue outlined a ReferralCode which belongs_to :user and I think we still want that.
This will rework the associations on the User model to has_one :referral_code, has_one :referrer, and has_many :referrals with the necessary through: options.
We can also add a unique constraint to the code on the new table in the database, along with the existing Rails validation.
|
Currently there is a |
|
Good point. I think that it should actually be |
126a060
to
a438034
Compare
|
@joemasilotti I updated the commit |
|
Thanks for the updates! I pulled down the code and I think I glossed over a major fallacy with this approach: it is terribly hard to reason around what is going on! Not your code, but the data modeling that I proposed. Thanks to your initial commit I'm leaning back towards getting rid of the I also think that I haven't given the naming conventions of this enough though. Here's what I'd love the naming to be:
Which I think means we need to add Does that make sense? Or am I overthinking this? Let me know if you'd like to give it another pass. And no worries if not – I understand I've jumped around a lot with this one! |
|
That comes pretty close to the commit I originally made. The main difference is that the relationship I used for the referrals made the user is referrals_made. |
a438034
to
126a060
Compare
|
I pushed the original commit to the remote branch. Take a look and let me know if it needs any adjustments. |
|
Awesome, thank you again! I pulled out the referral-related code to a module and tweaked the names a bit more. I think that in theory "referrer" and "referee" make sense but in practice I have a hard time wrapping my head around which is which. I like where this module Referrable
extend ActiveSupport::Concern
included do
has_one :referral, foreign_key: "referred_user_id", dependent: :destroy
has_one :referring_user, through: :referral
alias_method :referred_by, :referring_user
has_many :referrals, foreign_key: "referring_user_id", dependent: :nullify
has_many :referred_users, through: :referrals
validates :referral_code, uniqueness: true, allow_nil: true
end
end
user.referred_by # => User
user.referred_users # => [User] |
Closes #736
Pull request checklist
bin/check