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
是爲配置文件所附的每個項目創建的新評估嗎?如果是這樣,似乎評估應該'belongs_to:project' – 2011-05-23 23:36:47
項目整體就像一個連接器。例如:我評估了Project X的所有者,因爲我是Project X的成員。但由於評估鏈接將在'project#show'中,所以我想你可能是對的。 – Luk 2011-05-23 23:39:16