评估 - 反馈回路的多态关联

时间:2016-05-29 06:08:39

标签: ruby-on-rails ruby polymorphic-associations

我试图弄清楚如何在我的Rails 4应用程序中实现评估模型。

我之前曾问过这些相关问题,但尚未解决这个问题:

Rails 4 Polymorphic associations and concerns

Rails 4 - post completion evaluations model - structure

我有:

class Evaluation < ActiveRecord::Base

  belongs_to :evaluator, :polymorphic => true
  belongs_to :evaluatable, :polymorphic => true

我也对评估者提出了关注并评估为:

module Evaluator
    extend ActiveSupport::Concern

    included do 
        has_many :given_evaluations, as: :evaluator, dependent: :destroy, class_name: 'Evaluation'

    end
end
module Evaluatable
    extend ActiveSupport::Concern

    included do 
        has_many :received_evaluations, as: :evaluatable, dependent: :destroy, class_name: 'Evaluation'
    end
end

我在用户模型中包含了每个问题:

class User < ActiveRecord::Base
   include Evaluator
   include Evaluatable

在我的展示页面中,我想展示一个特定用户的评估(从其他用户那里获得 - 他们是评估者)。

在我的节目中,我有:

<% Evaluation.find(params[:id]).received_evaluations.order('created_at DESC').each do |eval| %>
                                <div id="portfolioFiltering" class="masonry-wrapper row">
                                        <%= eval.remark %>
                                        <%= eval.personal_score %>
                                        <small><%= eval.created_at %></small>

在我的评估表中,我不确定如何指定评估的收件人。我已经制作了基本表格,但我不清楚如何将它与应该接受评估的用户联系起来。

<%= simple_form_for(@evaluation) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :score, collection: 1..10, autofocus: true, :label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %>

    <%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10}  %>

我的评估表有:

    t.integer  "user_id"
    t.integer  "evaluatable_id"
    t.string   "evaluatable_type"
    t.integer  "overall_score"
    t.integer  "project_score"
    t.integer  "personal_score"
    t.text     "remark"
    t.boolean  "work_again?"
    t.boolean  "continue_project?"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
  end

  add_index "evaluations", ["evaluatable_type", "evaluatable_id"], name: "index_evaluations_on_evaluatable_type_and_evaluatable_id", unique: true, using: :btree

在我的用户控制器中,我有:

def show
    # authorize! :read, @user
    @received_evaluations = @user.received_evaluations
  end

我目前收到的错误是:

undefined method `received_evaluations' for #<Evaluation:0x007fb8c4b32160>

我不确定此消息的含义或解决方法。

如果我将节目改为:

<% @user.received_evaluations.each do |eval| %>

我收到此错误:

undefined method `received_evaluations' for nil:NilClass

1 个答案:

答案 0 :(得分:0)

在这个问题上阅读您的不同问题我认为正在尝试为一个User设置机制来评估另一个User

基于上述逻辑,不需要使用多态关联。它们可能会让你过于复杂。

这是我认为你需要的:

class User
  has_many :given_evaluations, foreign_key: :evaluator_id, class_name: Evaluation
  has_many :received_evaluations, foreign_key: :evaluatee_id, class_name: Evaluation
end

class Evaluation
  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模块。

因此,现在要显示用户收到的评估并删除您收到的错误消息:

在控制器中:

# ensure we get the user for all relevant routes
before_filter :get_user, only: [:show, edit, :update, :destroy]

def get_user
  @user = User.find(params[:id])
end

def show
  # get the received evaluations and order
  @received_evaluations = @user.received_evaluations
  @received_evaluations = @received_evaluations.order('created_at DESC')

  # you will probably want grab the evaluator record as well...
  @received_evaluations = @received_evaluations.includes(:evaluator)
end

并在显示视图

<% @received_evaluations.each do |eval| %>
  <div id="portfolioFiltering" class="masonry-wrapper row">
    <%= eval.remark %>
    <%= eval.personal_score %>
    <small><%= eval.created_at %></small>
    ...
  </div>
<% end %>