RoR求和问题

时间:2017-03-18 10:05:33

标签: html ruby-on-rails ruby

我的Ruby应用程序存在问题。我创建了与资源(resource_id)关联的用户。用户的工作时间表示资源的容量。资源表应显示资源的总容量。资源的总容量来自分配给相应资源的用户的容量之和。但是,在资源表中,为每个资源显示相同的容量(所有资源的总和)。

作为一个例子:

我有2次使用,每次使用8小时:

用户1:容量8,resource_id 1 用户1:容量8,resource_id 2

现在资源表中应该有2个资源,每个资源的容量为8.但是我的两个资源都有16个容量。

以下是相应的索引文件,控制器和模型。

resources.index

<% provide(:title, 'Ressourcen') %>
<p id="notice"><%= notice %></p>

<h1>Ressourcen</h1>

<table id="resources" class="display">
  <thead>
    <tr>
      <th>Name</th>
      <th>Gesamtkapazität</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>

  <% @resources.each do |resource| %>
    <tr>
      <td><%= resource.name %></td>
      <td><%= User.sum(:capacity, :conditions => {:resource_id => resource}) %></td>
      <td><%= link_to 'Anzeigen', resource %></td>
      <td><%= link_to 'Entfernen', resource, method: :delete, data: { confirm: 'Sind Sie sich sicher?' } %></td>
  <% end %>
  </tr>
  </tbody>
</table>

resource.model:

class Resource < ActiveRecord::Base
  def resource_params
    params.require(:resource).permit(:created_at, :name, :ocr, :cost, :oce)
  end

  validates :name, presence: true, uniqueness: true, length: {maximum: 50}

  has_many :procedure_resources, :dependent => :destroy
  has_many :procedures, through: :procedure_resources
  has_many :users, :dependent => :destroy

end

resource.controller:

class ResourcesController < ApplicationController
  before_action :set_resource, only: [:show, :edit, :update, :destroy]

  # GET /resources
  # GET /resources.json
  def index
    @resources = Resource.all
    @procedures = Procedure.all
    @project = Project.find(1)
  end


  def show
    @resource = Resource.find(params[:id])
  end

  private

  def set_resource
    @resource = Resource.find(params[:id])
  end

  def resource_params
    params.require(:resource).permit(:name, :oce, :oce, :cost, :ocr)
  end
end

users.model:

class User < ActiveRecord::Base

  belongs_to :resource

有谁知道我如何管理它,它只显示resource.index中相应资源的容量? 错误必须在resource.index中,并带有以下行:

<td> <% = User.sum (: capacity,: conditions => {: resource_id => resource})%> </ td>

祝你好运, 菲利普

0 个答案:

没有答案