Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Rails Referral system

has_many :through has_one :through

  • Please log in to reply
6 replies to this topic

#1 Dascalu

Dascalu

    Passenger

  • Members
  • 5 posts

Posted 16 April 2014 - 01:52 PM

Hi,

I'm trying to create a referral system i've maked a relationship model so far and need to know if it is good.

 

Migration: create_user_relationships.rb

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

Model: user.rb

class User < ActiveRecord::Base

	has_many :referrals, through: :userrelationships
	has_one :referrer, through: :userrelationships
end

Model: user_relationship.rb

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.

 

Thanks !



#2 Jemagee

Jemagee

    Inspector

  • Members
  • 62 posts

Posted 16 April 2014 - 02:09 PM

Why are you using a cross join table. 

 

If a user only has one referrer and referrer is a user - you can create a key within user itself.

 

belongs_to :referrer, class_name "User"

 

has_many :referrals, class_name "User", foreign_key: :referral_id

 

Your really only need cross joins when you have has many on both sides.

 

Think of it this way

 

You have a sports league.

 

There's a game table

 

There's a player table

 

A game has many players

a player has many games

 

You need a cross join table to determine what players played in what games (and their statistics from those games if you're tracking it as well)

 

You don't need a cross join if one user only has one referrer - just use the self join.

 

 


  • Dascalu likes this

#3 Dascalu

Dascalu

    Passenger

  • Members
  • 5 posts

Posted 16 April 2014 - 02:15 PM

Thanks a lot for your answer !

I'll do it your way , thanks !



#4 Dascalu

Dascalu

    Passenger

  • Members
  • 5 posts

Posted 17 April 2014 - 11:55 AM

Hi, it's me again.

I have another question on this matter and i thought that is better to write here that open a new topic.

 

My question is how can i do what i first wanted to do ? @Jemagee solution works , but i want also to know when that relationship referral-referrer was created and a few more things , so i think it's batter to use another table so i don't overpopulate the user table witch can be not good for my query time.

 

So, how can i create a relationship table that works in the principle User has many referrals(that are also users) , and a User has one referrer?

 

Thanks!



#5 Jemagee

Jemagee

    Inspector

  • Members
  • 62 posts

Posted 17 April 2014 - 01:09 PM

Well - the relatoinship of the refereree to the referer would be defined by the creation date wouldn't it?

 

The principle is that to use a cross join table if you want to have extra data



#6 Dascalu

Dascalu

    Passenger

  • Members
  • 5 posts

Posted 17 April 2014 - 01:28 PM

Yes but it's a app where the referrals can be sold (like items), so the creation date of a user instance will not necessarily mean the same date as the referral-referrer relationship creation. Also i want to store the acquire price for that referral, That's why i need a cross join table.

 

So anyone have an idea ?



#7 Dascalu

Dascalu

    Passenger

  • Members
  • 5 posts

Posted 25 April 2014 - 02:18 PM   Best Answer

So i'm back with a solution , after days of learning more about rails...here we go:

 

Migration for UserRelationship model:

class CreateUserRelationships < ActiveRecord::Migration
  def change
    create_table :user_relationships do |t|
      t.belongs_to :referrer
      t.belongs_to :referral

      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
 
  

UserRelationship Model:

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

And User Model:

class User < ActiveRecord::Base

	has_many :user_relationships, :foreign_key => 'referrer_id'
	has_many :referrals, :through => :user_relationships
	
	has_one	:reverse_user_relationship, 	:foreign_key => 'referral_id',
																				:class_name => 'UserRelationship'
	has_one :referrer, :through => :reverse_user_relationship
	
#....

end






Also tagged with one or more of these keywords: has_many :through, has_one :through

1 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users


    Yahoo (1)