【问题标题】:Rails 4 - post completion evaluations model - structureRails 4 - 完成后评估模型 - 结构
【发布时间】:2016-05-28 09:02:51
【问题描述】:

我仍然在使用 Rails。

我正在尝试向基于项目的应用程序添加评估功能。

我希望每个项目参与者在项目完成后提交评估。

我有一个评估模型:

评估.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

我担心:

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

然后我尝试将每个用户的评价(收到)显示为:

<% 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 %>  

但我收到此错误:

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

我什至不确定我是否理解了错误消息,更不用说弄清楚该怎么办了。

谁能理解这条信息?

【问题讨论】:

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


    【解决方案1】:

    从你的关系中删除它

    :polymorphic => true,
    

    这是一篇我们应该使用多态关系的文章。 http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 2017-03-24
    • 2020-03-06
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多