I'm trying to create a referral system i've maked a relationship model so far and need to know if it is good.
class CreateUserRelationships < ActiveRecord::Migration def change create_table :user_relationships do |t| t.integer :referral_id t.string :referrer_id t.timestamps end add_index :user_relationships, :referral_id add_index :user_relationships, :referrer_id add_index :user_relationships, [:referral_id, :referrer_id], unique: true end end
class User < ActiveRecord::Base has_many :referrals, through: :userrelationships has_one :referrer, through: :userrelationships end
class UserRelationship < ActiveRecord::Base belongs_to :referral, class_name: "User" belongs_to :referrer, class_name: "User" validates :referral_id, presence: true validates :referrer_id, presence: true end
I want to know if this is good so far , and i also have a question , i don't really know how to make a record in the user_relationship table when a player registers , some ideas please.