【问题标题】:Evaluation - polymorphic associations on feedback loop评估 - 反馈回路上的多态关联
【发布时间】:2016-05-29 06:08:38
【问题描述】:

我正在尝试弄清楚如何在我的 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

我还对 evaluator 和 evaluable as 提出了担忧:

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

【问题讨论】:

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


    【解决方案1】:

    阅读您对此问题的不同问题,我认为正在尝试为一个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 %>
    

    【讨论】:

    • 当我尝试此操作时,我创建了一个新的评估并收到一条错误消息:找不到具有 'id'=3 的用户。每次我制作一个新的,数字都会增加 1。我不认为这些是用户 ID,而是评估 ID。我不确定这里出了什么问题。
    • 上述动作在评估控制器中。我是否需要向用户控制器添加一些内容才能使其正常工作?
    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2016-06-18
    • 1970-01-01
    相关资源
    最近更新 更多