我如何只显示本周的任务 - Rails

时间:2016-03-22 22:09:41

标签: ruby-on-rails ruby-on-rails-4

我想只显示本周的任务,但到目前为止我只能对它们进行分组。

观点:

      <% @task_months.each do |month, tasks| %>
      <% for task in tasks %>
        <tr>
          <td><%= link_to task.name, task %></td>
          <%if task.due_date? %>
            <td>due on <%= task.due_date.strftime("%B %d, %Y")  %></td>
          <% end %>
          <td><%= link_to 'Edit', edit_task_path(task), class: "btn btn-info" %></td>
          <td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
        </tr>
        <% end %>
      <% end %>

控制器:

def week_task
@tasks = Task.order(:due_date)
  @task_months = @tasks.group_by {|t| t.due_date.beginning_of_week }
end

我正在使用设计,我还希望显示current_user和本周的@tasks的任务。

1 个答案:

答案 0 :(得分:0)

试试这个:

@task_months = @tasks.where('due_date >= ? AND due_date <= ?', Time.current.beginning_of_week, Time.current.end_of_week)

对于当前的用户任务,例如:

@task_months = @tasks.where('user_id = ?', current_user.id)

或者将这两者结合起来用于本周当前用户的任务:

@task_months = @tasks.where('due_date >= ? AND due_date <= ? AND user_id = ?', Time.current.beginning_of_week, Time.current.end_of_week, current_user.id)