新手问题 关于 Rails 外键的问题

wu · August 09, 2017 · Last by Loyea replied at August 16, 2017 · 1592 hits

论坛系统中会有一个关注其他用户的功能。首先有一个用户表(users)然后有一个关注表(follow)follow 表需要两个字段 follower_id 关注者、following_id 被关注者。这两个字段的外键都是 users 表的 id。这种情况迁移怎么写?

Follow Model 里加上下面代码应该就可以了吧

可以看看 https://ruby-china.org/topics/19619

belongs_to :follower, :class_name => "User", :foreign_key => "follower_id"  #关注者
belongs_to :following, :class_name => "User", :foreign_key => "following_id"  #被关注者

through 可以吧

Reply to will_c_j

好的,谢谢。我试试

这问题看完我还以为是我问的。。。。新手遇到场景一模一样

You need to Sign in before reply, if you don't have an account, please Sign up first.