2011-05-23 76 views
3
class Profile 
    has_many :projects, :through => "teamss" 
    has_many :teams, :foreign_key => "member_id" 
    has_many :own_projects, :class_name => "Project", :foreign_key => :profile_id 
    has_many :own_teams, :through => :own_projects, :source => :teams 
end 

class Project 
    belongs_to :profile, :class_name => "Profile" 
    has_many :teams 
    has_many :members, :class_name => "Profile", :through => "teams", :foreign_key => "member_id" 
end 

class Team 
    belongs_to :member, :class_name => 'Profile' 
    belongs_to :project 
end 

我需要創建模型Evaluation。我想要做的是在project#view頁面爲每個項目成員(包括所有者)生成一個鏈接,以便製作一個Evaluation此人將點擊鏈接並評估與此鏈接相關的人員。 Project的所有者將評估所有成員,所有成員將評估所有者。幫助在Rails中建立模型

我已經定義模式Evaluation如下,但我想我錯過了什麼:

class Evaluations < ActiveRecord::Base 
    belongs_to :evaluated, :class_name => 'Profile', :foreign_key => "evaluated_id" 
    belongs_to: :profile, :class_name => 'Profile', :foreign_key => "profile_id" 
end 

記住,Evaluation表將有萬噸的屬性,這就是爲什麼我會不has_many_and_belongs_to_many去。

如何創建此模型以便做我想做的事,並且能夠通過project#show頁面獲取我需要的所有內容?

謝謝!

編輯

變化做:

class Profile 
    has_many :evaluations, :dependent => :destroy, :foreign_key => :evaluation_id 
    has_many :evaluators, :through => :evaluations, :foreign_key => :profile_id 
end 

class Project 
    has_many :evaluations,:foreign_key => "project_id" 
end 

class Evaluations < ActiveRecord::Base 
    belongs_to :evaluated, :class_name => 'Profile', :foreign_key => "evaluated_id" 
    belongs_to: :profile, :class_name => 'Profile', :foreign_key => "profile_id" 
    belongs_to: :project, :class_name => 'Project', :foreign_key => "project_id" 
end 
+0

是爲配置文件所附的每個項目創建的新評估嗎?如果是這樣,似乎評估應該'belongs_to:project' – 2011-05-23 23:36:47

+0

項目整體就像一個連接器。例如:我評估了Project X的所有者,因爲我是Project X的成員。但由於評估鏈接將在'project#show'中,所以我想你可能是對的。 – Luk 2011-05-23 23:39:16

回答

0

你有你的團隊和個人資料(會員)協會有點混亂。一個團隊有許多配置文件(成員),一個配置文件(成員)屬於一個團隊。除此之外,你可能只是過分複雜的事情。我很好奇你爲什麼不直接打電話給Profile Model Member並讓事情變得簡單些?

這就是說,你正在做一個相當複雜的一組關聯。我強烈建議您仔細閱讀ActiveRecord Associations doc以深入瞭解這些事情的工作方式,特別是如何自動確定主鍵和外鍵。

我想下面會給你你想要的。

class Team 
    has_and_belongs_to_many :members, 
    :class_name => "Profile" 
    belongs_to :project 
    belongs_to :team_leader, 
    :class_name => "Profile" 
end 

class Profile 
    has_and_belongs_to_many :teams 
    has_many :projects, 
    :through => :teams 
    has_many :owned_projects, 
    :class_name => "Project", 
    :foreign_key => "owner_id" 
    has_many :owned_teams, 
    :class_name => "Team", 
    :foreign_key => "team_leader_id" 
    has_many :evaluations 
    has_many :evaluations_given, 
    :class_name => "Evaluation", 
    :foreign_key => "evaluator_id" 
end 

class Project 
    has_many :teams 
    belongs_to :owner, 
    :class_name => "Profile" 
    has_many :members, 
    :through => :teams 
    has_many :evaluations 
end 

class Evaluation 
    belongs_to :profile 
    belongs_to :evaluator, 
    :class_name => "Profile", 
    :foreign_key => "evaluator_id" 
    belongs_to :project 
end 

爲了支持這些模型定義將是下表/鍵:

evaluation 
    id 
    profile_id 
    evaluator_id 
    project_id 

teams 
    id 
    project_id 
    team_leader_id 

profiles 
    id 

project 
    id 
    owner_id 

profiles_teams 
    profile_id 
    team_id 

有了這個,你應該能夠訪問的集合/,你需要得到正確的鏈接,如對象:

  • project.members

    project.owner

    project.teams

    team.project

    profile.projects

    profile.owned_projects

    profile.teams

    team.members

    隊。領導

    project.evaluations

    profile.evaluations

    evaluation.evaluator

    profile.evaluations_given

另請注意,您嵌套資源將是同樣複雜,如果您嘗試留在RESTFUL框架。閱讀!