Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Self Join in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.66k
    Comment on it

    In Self join association one model has relation with itself. For example one user table can have list of all coach and player. One coach can train many players. We will represent the association as:

    class User < ActiveRecord::Base
      has_many :players, :class_name => "User",  :foreign_key => "coach_id"
      belongs_to :coach, :class_name => "User"
    end
    

    With the above self join association we can retrieve all the players under a coach as @coach.players and coach of a player as @player.coach.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: