范围将两个 "Categories" 分成两个索引表?

Scope to Break Two "Categories" Into Two Index Tables?

当用户对自己进行量化时,_form 会为他们提供两种方法来对结果进行分类,即每月 平均值 或 one-time 实例。我希望每个类别都有自己的 table,如下所示:

我如何使用范围来执行此操作,因为现在我从第一个代码片段中收到未定义的方法错误:@average_quantifieds = current_user.quantifieds.average & @instance_quantifieds = current_user.quantifieds.instance

quantifieds_controller.rb

class QuantifiedsController < ApplicationController
  before_action :set_quantified, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
   @quantifieds = Quantified.all.order("date")
   @average_quantifieds = current_user.quantifieds.average
   @instance_quantifieds = current_user.quantifieds.instance
  end

  def show
  end

  def new
    @quantified = current_user.quantifieds.build
  end

  def edit
  end

  def create
    @quantified = current_user.quantifieds.build(quantified_params)
    if @quantified.save
      redirect_to quantifieds_url, notice: 'Goal was successfully created'
    else
      render action: 'new'
  end
end

  def update
    if @quantified.update(quantified_params)
      redirect_to quantifieds_url, notice: 'Goal was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @quantified.destroy
    redirect_to quantifieds_url
  end

  private
    def set_quantified
      @quantified = Quantified.find(params[:id])
    end

    def correct_user
      @quantified = current_user.quantifieds.find_by(id: params[:id])
      redirect_to quantifieds_path, notice: "Not authorized to edit this goal" if @quantified.nil?
    end

    def quantified_params
      params.require(:quantified).permit(:categories, :name, :metric, :result, :date)
    end
end

quantified.rb

class Quantified < ActiveRecord::Base
    belongs_to :user

    CATEGORIES = ['Monthly Average', 'One-Time Instance']
end

索引

<table>
    <% @average_quantifieds.each do |average| %>
      ....
    <% end %>
</table>

<table>
    <% @instance_quantifieds.each do |instance| %>
        ....
    <% end %>
</table>

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

    has_many :goals
    has_many :values
    has_many :quantifieds
end

在 rb 文件中:

class Quantified < ActiveRecord::Base
 belongs_to :user
  scope :averaged,  -> { where(categories: 'Monthly Average') }
  scope :instance, -> { where(categories: 'One-Time Instance') }

 CATEGORIES = ['Monthly Average', 'One-Time Instance']
end

我将所有内容从 "average" 更改为 "averaged",否则我会收到一条错误消息。