问题描述
我仍然对Rails感到满意.
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
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
我担心:
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 %>
但是我得到这个错误:
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?
推荐答案
从您的关系中删除
:polymorphic => true,
这是一篇我们应该使用多态关系的文章. http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
Here is a article when we should use polymorphics relations.http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
这篇关于Rails 4-完成后评估模型-结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!