一種方法是有一個單獨的ProjectOwner
和ProjectUser
模型兩者都是繼承於User
模型。 和has_and_belongs_to_manyProject
和ProjectUser
模型之間的關聯。
class Project < ActiveRecord::Base
belongs_to :project_owner
has_and_belongs_to_many :project_users
end
class User < ActiveRecord::Base
has_secure_password
# other common attributes here
end
class ProjectOwner < User
has_many :projects
end
class ProjectUser < User
has_and_belongs_to_many :projects
end
在projects
表中添加project_owner_id
列。
創建一個連接表project_users_projects
這將是這樣的:
create_table :project_users_projects, id: false do |t|
t.belongs_to :project_user, index: true
t.belongs_to :project, index: true
end
然後,你就可以做這樣的事情:
project_owner = project.project_owner
project_owner_projects = project_owner.projects
project_user_projects = project_user.projects
project_users = project.project_users
這在我看來是一個'has_and_belongs_to_many」的關係。 – Daiku