Ruby on Rails has_many through self-referential following/follower relationships

后端 未结 2 2060
天命终不由人
天命终不由人 2020-12-28 15:49

There are a number of posts and threads on has_many :through, but I haven\'t found any that cover specifically what I\'m trying to do.

I have a User model and a Fri

2条回答
  •  隐瞒了意图╮
    2020-12-28 16:22

    I'm kind of a noob in a learning process, but this models seem cleaner to me:

    class User < ActiveRecord::Base
      has_many :followings
      has_many :followers, through: :followings
    end
    
    class Following < ActiveRecord::Base
      belongs_to :user
      belongs_to :follower, class_name: 'User'
    end
    
    • Source Treehouse : https://www.youtube.com/watch?v=jSUWu50XK48

提交回复
热议问题