Rails 5:从多个表中检索数据

时间:2018-10-25 08:58:25

标签: jquery ruby-on-rails json ruby-on-rails-5

当前,我正在尝试将多个表中的某些数据显示到我的日历中。

问题是我想显示当前日期的订单总数,但是我没有创建一个表来计算订单总数。

是否可以计算特定日期(如.collect)的所有订单?

还是我必须编写自定义方法?

这是我的日历控制器:

class CalendarsController < ApplicationController
  before_action :authenticate_user!
  include ApplicationHelper

  def host
    @wines = current_user.wines

    params[:start_date] ||= Date.current.to_s
    params[:wine_id] ||= @wines[0] ? @wines[0].id : nil

    if params[:q].present?
      params[:start_date] = params[:q][:start_date]
      params[:wine_id] = params[:q][:wine_id]
    end

    @search = Reservation.ransack(params[:q])

    if params[:wine_id]
      @wine = Wine.find(params[:wine_id])
      start_date = Date.parse(params[:start_date])

      first_of_month = (start_date - 1.months).beginning_of_month
      end_of_month = (start_date + 1.months).beginning_of_month

      @events = @wine.reservations.joins(:user)
                      .select('reservations.*, users.fullname, users.email')
                      .where('(start_date BETWEEN ? AND ?) AND status <> ?', first_of_month, end_of_month, 2)

      # @events.each{ |e| e.image = avatar_url(e) }

    else
      @wine = nil
      @events = []
    end
  end
end

我的host.html.erb:

<div class="container-small">
<% if !@wines.blank? %>
  <div class="row">
    <%= search_form_for @search, class: "form-group", remote: true, url: host_calendar_path do |f| %>
      <div class="col-md-6 select">
        <div class="form-group">
          <label>Deine Weine</label>
          <%= f.select :wine_id, options_for_select(@wines.collect {|u| [u.wine_name, u.id]}, params[:wine_id]), {}, {
            onchange: "$(this.form).submit()",
            class: "form-control"
            } %>
        </div>
      </div>

      <%= f.hidden_field :start_date, id: "start-date", value: params[:start_date], onchange: "$('this.form').submit()" %>

    <% end %>
  </div>
<% end %>

<div id="calendar"></div>
</div>
<script>
  window.reservations = <%= raw @events.to_json %>
  console.log(reservations);

  function showReservations(data) {
    return data.map(function (e) {
      if (e['start_date'] !== e['end_date']) {
        e['end_date'] = moment.utc(e['end_date']).add(1, 'days')
      }

      return {
        name: e.fullname,
        total: e.total,
        email: e.email,
        start: e['start_date'],
        end: e['end_date'],
        avatar: e.image,
        status: e.status
      }
    });
  }


  $('#calendar').fullCalendar({
    header: {
      left: 'title',
      center: '',
      right: 'prev,next'
    },
    defaultDate: $('#start-date').val(),
    events: showReservations(reservations),
    eventRender: function(event, element, view) {
      return $(`
        <a class="fc-day-grid-event fc-h-event fc-event fc-start fc-end">
          <div class="fc-content ${event.status}">
            <span class="fc-title">${event.total}</span>
          </div>
        </a>
        `);
    }
  });

  $('.fc-prev-button').click(function(){
    var current = new Date($('#start-date').val());
    var prev = new Date(current.getFullYear(), current.getMonth() - 1, 1)
    $('#start-date').val(moment(prev).format('YYYY-MM-DD'))
    $('#start-date').trigger('change')
  });

  $('.fc-next-button').click(function(){
    var current = new Date($('#start-date').val());
    var next = new Date(current.getFullYear(), current.getMonth() + 1, 1)
    $('#start-date').val(moment(next).format('YYYY-MM-DD'))
    $('#start-date').trigger('change')
  });

</script>

0 个答案:

没有答案