如何基于rails中的月份搜索数据

时间:2018-06-20 17:12:35

标签: ruby-on-rails

我需要根据以下内容搜索数据

  1. 按月
  2. 四分之一
  3. 每半年一次
  4. 按年度显示在我的收入报告中。

有人可以帮我怎么做吗?我的收入报告显示预订信息 在控制器中。

def revenue
  @bookings = Booking.all
  @bookings = Booking.where(currency: params[:currency]) if param[:currency].present?
end

我的Revenue.html.erb

<div class="col-md-3 col-lg-2 col-xlg-1">
 <%= form_tag revenue_path, :method => 'get' do %>
    <%= select_tag(:currency, options_for_select(["USD", "EUR"]), onchange: 'this.form.submit();', class: "form-control custom-select" , include_blank: true ) %>
 <% end %>   
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
  <%= select_month(Date.today)%>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
     <select class="custom-select" data-style="form-control btn-secondary">
    <option>By quater</option>
  </select>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
  <select class="custom-select" data-style="form-control btn-secondary">
    <option>By Halfyear</option>
  </select>
 </div>
 <div class="col-md-6 col-lg-2 col-xlg-2">
  <select class="custom-select" data-style="form-control btn-secondary">
    <option>By Year</option>
  </select>
 </div>

 <div class="table-responsive">
  <table id="demo-foo-addrow" class="table m-t-30 table-hover no-wrap contact-list no-paging footable-loaded footable" data-page-size="2">
    <thead>
        <tr>
            <th class="footable-sortable">#<span class="footable-sort-indicator"></span></th>
            <th class="footable-sortable">
                <input type="checkbox" class="check" id="minimal-checkbox-1">
            </th>
            <th class="footable-sortable">First Name<span class="footable-sort-indicator"></span></th>
            <th class="footable-sortable">Last Name<span class="footable-sort-indicator"></span></th>
            <th class="footable-sortable">Email<span class="footable-sort-indicator"></span></th>
            <th class="footable-sortable">Status<span class="footable-sort-indicator"></span></th>
        </tr>
    </thead>
    <tbody>
        <% @bookings.each do |booking| %>
        <tr>
            <td><%= booking.id %></td>
            <td><%= check_box_tag "bookings[booking_ids][]", booking.id %></td>
            <td><%= booking.first_name %></td>
            <td><%= booking.last_name %></td>
            <td><%= booking.email %></td>
            <td><%= booking.status %></td>
        </tr>
        <% end %>
    </tbody>
  </table>
 </div>

谁能帮助我。预先感谢!

1 个答案:

答案 0 :(得分:0)

基于与OP一起进行调试,发现给定方法实际上比写的要执行更多的工作。在方法的最后一行中,@booking = Booking.paginate(:per_page => 100)已完成,它溶解了之前生成的查询集。

最终的变体如下所示:

def revenue
    @bookings = Booking.all
    if params[:currency].present?
      @bookings = @bookings.where(currency: params[:currency])
    end
    if params[:search].present?
      @bookings = @bookings.where('first_name like ?', "%#{params[:search]}}")
    end

    if params['date'] && params['date']['month']
      @selected_date = Date.parse('2018-'+ params['date']['month'].to_s + '-01')
      @bookings = @bookings.where('extract(month from created_at) = ?', params['date']['month'])
    end

    if params['date'] && params['date']['year']
      @selected_year = params['date'] && params['date']['year'] ? Date.parse( params['date']['year'].to_s + '-01') : Date.today
      @bookings = Booking.where('extract(year  from created_at) = ?',  params['date']['year'])
    end

    @per_page = params[:per_page] || Booking.per_page || 20
    @bookings = @bookings.paginate(:per_page => @per_page, :page => params[:page])
end