除了一些小错误之外,您基本上是正确的:
attr_accessible :relations_id
是多余的。将其从您的Relationship
model.
Both Relationship
and User
模特来电has_many
彼此交往。Relationship
应该打电话belongs_to
因为它包含外键。
In your User
模型、套装:foreign_key => "follower_id"
.
我将这样做。
Have a Follow
具有多态关联的中产阶级followable
内容方面和 has_many 方面follower
用户端(用户有很多关注者)。
首先,创建一个follows
table:
class CreateFollows < ActiveRecord::Migration
def change
create_table :follows do |t|
t.integer :follower_id
t.references :followable, :polymorphic => true
t.timestamps
end
end
end
Replace Relationship
模型与一个Follow
model:
class Follow < ActiveRecord::Base
belongs_to :followable, :polymorphic => true
belongs_to :followers, :class_name => "User"
end
包括User
model:
has_many :follows, :foreign_key => :follower_id
包括在您的三个可关注的课程中:
has_many :follows, :as => :followable
您现在可以执行以下操作:
TheContent.follows # => [Follow,...] # Useful for counting "N followers"
User.follows # => [Follow,...]
Follow.follower # => User
Follow.followable # => TheContent