好的,开始吧。我不知道我是不是把事情复杂化了,还是我对Rails还很陌生,以至于不了解基本知识。我想要的sudo代码是这样的:
User
has_many projects as owner through relationship
has_many projects as contributor through relationship
has_many projects as follower through relationship
Project
has_one user as owner through relationship
has_many users as followers through relationship
has_many users as contributors through relationship
Relationship
belongs_to user
belongs_to project
然后我想拥有以下神奇的东西:
owner = Project.owner
followers = Project.followers
contributors = Project.contributors
projects = User.projects
myprojects = User.projects... (now I'm really not sure)
followedProjects = ...
contributingProjects = ...
所以在写下来的时候,我可以看到我对这个模型的理解还有另一个差距。用户可以扮演所有者、追随者或贡献者的角色,也可以是这三者的任意组合。
就实际代码而言,我在这里添加了我认为相关的部分:
class User < ActiveRecord::Base
has_many :user_project_relationships, :as => :relateable, :class_name => "UserProjectRelationship"
has_many :projects, :as => :owner, :through => :relateable, :class_name => "Project", :source_type => :owner
has_many :projects, :as => :follower, :through => :relateable, :class_name => "Project", :source_type => :follower
has_many :projects, :as => :contributor, :through => :relateable, :class_name => "Project", :source_type => :contributor
end
class Project < ActiveRecord::Base
has_many :user_project_relationships, :as => :relateable, :class_name => "UserProjectRelationship"
has_one :user, :as => :owner, :through => :relateable, :class_name => "User"
has_many :users, :as => :followers, :through => :relateable, :source_type => :follower, :class_name => "User"
has_many :users, :as => :contributors, :through => :relateable, :source_type => :contributor, :class_name => "User"
end
class UserProjectRelationship < ActiveRecord::Base
belongs_to :user
belongs_to :project, :polymorphic => true
end
关系模型的迁移是:
class CreateUserProjectRelationships < ActiveRecord::Migration
def self.up
create_table :user_project_relationships do |t|
t.integer :relateable_id
t.string :relateable_type
t.integer :project_id
t.timestamps
end
add_index :user_project_relationships, [:relateable_id, :relateable_type], :name => :relateable
add_index :user_project_relationships, :project_id
end
def self.down
drop_table :user_project_relationships
end
end
目前,我在project.users ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :relateable in model Project
等方面遇到错误
我觉得我在这里太荒无人烟了,无法真正得到我想要的,也许依靠神奇的栏杆可以做得更多。任何关于最佳路径的指导都将不胜感激。
提前感谢
Steve
Rails可以做很多事情,但我认为您试图让Relationship模型做得太多了。每一种关系都是不同的,所以我想尽量保持这种关系
将其拆分为单独的连接模型:
class User < ActiveRecord::Base
has_many :owned_projects, :class_name => "Project", :foreign_key => :owner_id
has_many :projects_followers
has_many :followed_projects, :class_name => "Project", :through => :projects_followers
has_many :projects_contributors
has_many :contributed_projects, :class_name => "Project", :through => :projects_contributors
end
class Project < ActiveRecord::Base
belongs_to :owner
has_many :projects_followers
has_many :followers, :class_name => "User", :through => :projects_followers
has_many :projects_contributors, :foreign_key => :contributor_id
has_many :contributors, :class_name => "User", :through => :projects_contributors
end
class ProjectsFollowers < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
class ProjectsContributors < ActiveRecord::Base
belongs_to :user
belongs_to :project
end
应该更接近你想要的。然后你确实可以做
project.owner
project.followers
project.contributors
和
user.owned_projects
user.followed_projects
user.contributed_projects
这要么有效,要么让你非常接近。
我认为你的混乱是因为试图建立多态关系,我认为这在这里是不可取的。AFAI grok,多态关系的用例是当您希望1个模型以相同的方式与任何数量的其他模型相关时。这里的情况并非如此,因为您有两个模型,它们之间有3种不同类型的关系。