Rails 4 - post completion evaluations model - structure

天涯浪子 提交于 2020-01-17 01:22:49

问题


I'm still feeling my way with Rails.

I'm trying to add an evaluation function to my projects based app.

I want each project participant to submit an evaluation when a project is complete.

I have an evaluation model with:

Evaluation.rb

# == Schema Information
#
# Table name: evaluations
#
#  id                :integer          not null, primary key
#  user_id           :integer
#  evaluatable_id    :integer
#  evaluatable_type  :string
#  overall_score     :integer
#  project_score     :integer
#  personal_score    :integer
#  remark            :text
#  work_again?       :boolean
#  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
#

class Evaluation < ActiveRecord::Base

  # --------------- associations

  belongs_to :evaluator, :polymorphic => true, class_name: 'Evaluation'
  belongs_to :evaluatable, :polymorphic => true, class_name: 'Evaluation'


  # --------------- scopes

  # --------------- validations

  # --------------- class methods

  # --------------- callbacks

  # --------------- instance methods

  # --------------- private methods

end

I have concerns for:

module Evaluatable
    extend ActiveSupport::Concern

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

module Evaluator
    extend ActiveSupport::Concern

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

    end
end

I'm then trying to show each user's evaluations (received) as:

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

But I get this error:

undefined method `evaluations' for #<Evaluation:0x007ff274b13b90>
Did you mean?  evaluator
               evaluator=

I'm not even sure I've understood the error message, let alone figured out what to do about it.

Can anyone make sense of this message?


回答1:


Remove this from your relations

:polymorphic => true,

Here is a article when we should use polymorphics relations. http://guides.rubyonrails.org/association_basics.html#polymorphic-associations



来源:https://stackoverflow.com/questions/37497137/rails-4-post-completion-evaluations-model-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!