您如何将 DateTime.now 与我的任务 table 模型中的日期时间列进行比较?

How do you compare DateTime.now to a datetime column in my task table model?

我正在 Rails 上使用 Ruby 创建任务管理器。

我的任务管理器有一个任务模型 table,其中包含一个名为 duedate 的列。 我需要能够找到所有逾期的任务。 如何编写一个函数,自动让我将所有逾期任务标记为 True?

这是我的架构。


  create_table "tasks", force: :cascade do |t|
    t.string "title"
    t.string "description"
    t.datetime "duedate"
    t.boolean "completed"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

我的任务控制器看起来像这样

class TasksController < ApplicationController
  before_action :set_task, only: %i[ show edit update destroy ]
  # before_action :due_today, only: %i[ index show]

  # GET /tasks or /tasks.json
  def index
    @tasks = Task.all.overdue.order(duedate: :asc)
  end

  # GET /tasks/1 or /tasks/1.json
  def show
  end

  # GET /tasks/new
  def new
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks or /tasks.json
  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: "Task was successfully created." }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /tasks/1 or /tasks/1.json
  def update
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to @task, notice: "Task was successfully updated." }
        format.json { render :show, status: :ok, location: @task }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1 or /tasks/1.json
  def destroy
    @task.destroy
    respond_to do |format|
      format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  def overdue?
    overdue < Time.now
  end

  # def overdue
  #   @tasks = Task.where(overdue: true)
  # end

  # def due_today
  #   @tasks = Task.where(duedate: = Date.now)
  # end

  private
        # Use callbacks to share common setup or constraints between actions.
  # def overdue?
  #   if(DateTime.now > duedate)
  #     @task.overdue = true
  #   else
  #     @task.overdue = false
  #   end
  # end

  # def overdue
  #   @tasks = Task.where(overdue: true)
  # end

    def set_task
      @task = Task.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def task_params
      params.require(:task).permit(:title, :description, :duedate, :completed, :overdue)
    end
end

我的模型是这样的

class Task < ApplicationRecord
  scope :overdue, -> { where("duedate < ?", Time.now) }

  def overdue?
    overdue < Time.now
  end
end

我建议从您的数据库中删除 overdue 列。它没有用,因为 duedate 列与当前时间的比较更加精确,并且不依赖于布尔列的手动设置。

您可以使用一个范围来查找所有过期的记录:

# in your model at app/models/task.rb
scope :overdue, -> { where("duedate < ?", Time.now) }

# in your controller
def index
  @tasks = Task.overdue.order(:duedate)
end

当您想在模型上使用 overdue? 方法时,例如,要在视图中显示过期标志,然后向模型添加 overdue? 方法:

# in your model at app/models/task.rb
def overdue?
  duedate < Time.now
end