在我的应用程序中,我有用户和项目的模型。
我希望用户能够关注许多项目。因此,用户拥有_许多项目,并且项目属于_不仅创建它们的用户,而且还有关注它们的用户。
所以我生成了一个名为的迁移ProjectRelationship
并试图让它在下面流动,但它似乎不起作用。有人可以帮我修复我的关联吗?
谢谢您的帮助!
project_relationship.rb
class ProjectRelationship < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
项目.rb
belongs_to :user
has_many :project_relationships
has_many :followers, through: :project_relationships, source: :user
用户.rb
has_many :projects
has_many :project_relationships
has_many :projects_followed, through: :project_relationships, source: :project
架构.rb
create_table "project_relationships", :force => true do |t|
t.integer "follower_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "projectuser_id"
end
add_index "project_relationships", ["follower_id"], :name => "index_project_relationships_on_follower_id", :unique => true
add_index "project_relationships", ["projectuser_id"], :name => "index_project_relationships_on_projectuser_id"
项目/show.html.erb
<%= @project.followers.count %>