how can I do self-reference with ruby on rails?

后端 未结 4 722
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 12:34

I want to self-referentiate a model in a RoR app but, I don\'t know exactly how. I want to save a linked list where the next node has the id of the previous one. how can I d

4条回答
  •  失恋的感觉
    2021-01-01 13:20

    I've spent some time trying to make it work using Rails 3.2.14

    The documentation's suggestion for self-joining associations hasn't worked for belongs_to associations. Adding a foreign key fixed the issue.

    Class User < ActiveRecord::Base
      has_many :invitees, class_name: 'User', foreign_key: :invited_by
      belongs_to :host, class_name: 'User', foreign_key: :invited_by
    end
    

提交回复
热议问题