2016-05-29 65 views
1

我對我試圖在Rails 4應用中實現反饋循環的嘗試感到困惑。Rails - 反饋循環模型 - 用戶作爲「評估者」和「可評估」

我也問過有關這個問題的問題在這裏:

Evaluation - polymorphic associations on feedback loop

我還在試圖找出一般的方法 - 和解決這些問題,但我有一個具體問題是關於如何設置與用戶關聯的模型。

我試圖銷燬所有用戶在我的軌道控制檯,但是我越來越停止了與一個錯誤,指出:

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column evaluations.evaluator_id does not exist 

我想這意味着我需要把「evaluator_id」列在我的評估表中。

目前,該表具有:

id    :integer   not null, primary key 
    user_id   :integer 
    evaluatable_id :integer 
    evaluatable_type :string 
    overall_score  :integer 
    continue_project? :boolean 
    created_at  :datetime   not null 
    updated_at  :datetime   not null 

Indexes 

    index_evaluations_on_evaluatable_type_and_evaluatable_id (evaluatable_type,evaluatable_id) UNIQUE 

在每種情況下,評估器爲用戶的別名。用戶也是評估的所有者。我不確定我是否真的需要將evaluateator_id添加到用戶,因爲我的模型上已經有用戶。

任何人都可以看到我應該做些什麼來解決這個問題嗎?

+0

你在這裏提供的是哪個表格/型號? – Maxence

+0

評價表 – Mel

回答

0

您的評估模型都屬於Evaluator和Evaluatable其他職位。但是你的表格似乎沒有評估者的外鍵...你應該創建一個遷移,使'參考'評估者

0

在這個問題上閱讀你的不同問題我認爲你試圖建立一個機制爲User評估另一個User

基於上述邏輯,不需要使用多態關聯。他們可能對你來說過於複雜。

這是我覺得會適合您的需求更好地:

class User < ActiveRecord::Base 
    has_many :given_evaluations, foreign_key: :evaluator_id, dependent: :destroy, class_name: Evaluation 
    has_many :received_evaluations, foreign_key: :evaluatee_id, dependent: :destroy, class_name: Evaluation 
end 

class Evaluation < ActiveRecord::Base 
    belongs_to :evaluator, foreign_key: :evaluator_id, class_name: User 
    belongs_to :evaluatee, foreign_key: :evaluatee_id, class_name: User 
end 

你的評價表會是這樣的:

id    :integer   not null, primary key 
evaluator_id  :integer   not null 
evaluatee_id  :integer   not null 
overall_score  :integer 
continue_project :boolean 
created_at  :datetime   not null 
updated_at  :datetime   not null 

然後,您可以廢除的EvaluatorEvaluatable模塊。

給定一個Evaluation,您可以訪問及接收到的評價,像這樣的User

evaluation = Evaluation.first 
evaluator = evaluation.evaluator # returns a User 
evaluatee = evaluation.evaluatee # returns a User 

,您可以訪問一個用戶給定的,並與收到的評價如下:

user = User.find(params[:id]) 
given_evaluations = user.given_evaluations 
received_evaluations = user.received_evaluations 

或者,如果您確實想分離出Evaluator和的功能3210你可以創建單獨的類,它們都訪問相同的users表。這更多的是單表繼承結構。

class User 
end 

class Evaluator < User 
    has_many :evaluations 
end 

class Evaluatee < User 
    has_many :evaluations 
end 

class Evaluation < ActiveRecord::Base 
    belongs_to :evaluator 
    belongs_to :evaluatee 
end 

給定一個Evaluation,您可以訪問用戶,像這樣:

evaluation = Evaluation.first 
evaluator = evaluation.evaluator # returns a Evaluator 
evaluatee = evaluation.evaluatee # returns a Evaluatee 

而且你可以訪問用戶提供下列給予和接受的評價:

user_id = params[:id] 
given_evaluations = Evaluator.find(user_id).evaluations 
received_evaluations = Evaluatee.find(user_id).evaluations 

# or 

given_evaluations = Evaluation.where(evaluator_id: user_id) 
received_evaluations = Evaluation.where(evaluatee_id: user_id) 

的警告這種方法是,評估記錄的依賴性破壞變得更加困難,因爲協會分爲兩個單獨的模型。

+0

嗨布倫特,我想我嘗試了這種方式,因爲我希望學生參與團體項目,然後爲對方留下反饋。評估屬於該項目以及用戶。您建議的方法是否意味着我無法將反饋分配給項目以及用戶? – Mel

+0

只是爲了確認,你能澄清一個學生正在評估什麼嗎?他們是否爲項目本身提供反饋意見,或向項目的個別成員反饋意見? – br3nt

+0

另一個學生和項目。對於項目,他們回答像'continue_project?'這樣的問題。如果他們想繼續努力的話。大多數問題都是其他參與者的反饋 – Mel