Rails 5:获取完整的订单数量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails 5:获取完整的订单数量相关的知识,希望对你有一定的参考价值。

我想在预订视图中显示所有订单和用户获得的总金额。

我尝试了Reservation.count(:all),但这似乎不起作用。有没有办法统计所有用户的订单?

这是我当前的预订控制器,没有任何更改:

class ReservationsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_reservation, only: [:approve, :decline]

  def create
    wine = Wine.find(params[:wine_id])

    if current_user == wine.user
      flash[:alert] = "Du kannst nicht deinen eigenen Wein kaufen!"
    else

    start_date = Date.parse(reservation_params[:start_date])

    @reservation = current_user.reservations.build(reservation_params)
    @reservation.wine = wine
    @reservation.price = wine.price
    @reservation.total = wine.price * @reservation.bottle
    # @reservation.save

  if @reservation.save
    if wine.Reservieren?
      flash[:notice] = "Anfrage versendet!"
  else
    @reservation.Bearbeitung!
    flash[:notice] = "Eroflgreich bestellt!"
  end
else
    flash[:alert] = "Can't make a reservation!"
  end

  end
    redirect_to wine
  end

  def your_orders
    @orders = current_user.reservations.order(start_date: :asc)
  end

  def your_reservations
    @wines = current_user.wines
      redirect_to root_path unless current_user.admin == true
  end

  def your_upcoming_reservations
    @wines = current_user.wines
      redirect_to root_path unless current_user.admin == true
  end


  def approve
    @reservation.Versendet!
    redirect_to your_reservations_path
    redirect_to root_path unless current_user.admin == true
  end

  def decline
    @reservation.Abgelehnt!
    redirect_to your_reservations_path
    redirect_to root_path unless current_user.admin == true
  end

  # Each Order Details
  def order_details
    @orders = current_user.reservations.order(start_date: :asc)
  end

  private

  def set_reservation
    @reservation = Reservation.find(params[:id])
  end

  def reservation_params
    params.require(:reservation).permit(:start_date, :bottle)
  end
end

这是我目前的观点:

<div class="container-small">
<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        Alle Bestellungen
      </div>
      <div class="panel-body">
        <% @wines.each do |wine| %>
          <% wine.reservations.each do |reservation| %>
            <div class="row">
              <div class="col-md-3">
                Erwartet am<br/>
                <%= reservation.start_date.strftime('%v') %>
              </div>
              <div class="col-md-2">
                <p><%= reservation.status %></p>
                <div class="form-inline">
                  <% if reservation.Bearbeitung? %>
                    <%= link_to approve_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-up fa-lg"></i> <% end %> |
                    <%= link_to decline_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-down fa-lg"></i> <% end %>
                  <% end %>
                </div>
              </div>
              <div class="col-md-4">
                <%= reservation.bottle %>x
                <%= link_to reservation.wine.wine_name, wine_path(reservation.wine) %><br/>
                Gesamt: <%= reservation.total %>€<br/>
              </div>
              <div class="col-md-3 pull-right">
                Lieferung an<br/><br/>
                <span>
                  <%= link_to user_path(reservation.user) do %>
                    <%= reservation.user.fullname %><br/>
                  <% end %>
                  <%= reservation.user.location %>
                </span>
              </div>
            </div>
            <hr/>
          <% end %>
        <% end %>
      </div>
    </div>
  </div>
</div>
</div>
答案

在控制器中,您应该致电:

@count = Reservation.count()
@total = Reservation.sum(:total)

然后在视图中:

Reservations Count: <%= @count %>
Reservations Total: $ <%= @total %>

适用于countsum的Ruby On Rails API文档

另一答案

用于明确表示您正在计算所有预留的替代语法是:

@count = Reservation.all.count

就个人而言,我希望尽可能明显地了解我期望从查询中得到什么。比如,如果将来您只想计算未取消或可能只计算特定月份预订的预订,该怎么办?

R. Sierra的其余答案就是重点。

以上是关于Rails 5:获取完整的订单数量的主要内容,如果未能解决你的问题,请参考以下文章

什么是在 C++ 中获取总内核数量的跨平台代码片段? [复制]

ArgumentError:升级 Rails 6.1.3 时出现错误数量的参数错误

ABAP采购订单如何取不同移动类型数量的值

Woocommerce - 获取订单商品的价格和数量。

Rails 5 缓存在本地或生产环境中不起作用

Rails 5 有很多通过实现