一共需要3个表:users、projects和project_editor_links
您的迁移:
create_table :users do |t|
# user stuff
end
create_table :projects do |t|
t.references :user
# project stuff
end
create_table :project_editor_links |t|
t.references :user
t.references :project
end
要从命令行生成最后一个表:
rails g migration project_editor_links project:references user:references
您的模型应如下所示:
class User < ActiveRecord::Base
has_many :projects
has_many :project_editor_links
has_many :edited_projects, :through => :project_editor_links
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :project_editor_links
has_many :editors, :through => :project_editor_links
end
class ProjectEditorLinks < ActiveRecord::Base
belongs_to :editor, :class_name => 'User', :foreign_key => :user_id
belongs_to :edited_project, :class_name => 'Project', :foreign_key => :project_id
end