查询没有显示正确的结果

时间:2011-06-23 12:53:49

标签: ruby-on-rails-3

在我的Rails 3项目中,我有

的以下代码
  1. 我的控制器:

    class TasksController < ApplicationController
    
      def today
        @tasks = Task.today
        @task = Task.new
        respond_to do |format|
          format.html { render :text=> "Sorry , you don't have any task pending today." }
    
          format.html # new.html.erb
          format.xml  { render :xml => @tasks }
        end
      end
    
      def this_week
        @tasks = Task.this_week
        @task = Task.new
        respond_to do |format|
          format.html { render :text => "Sorry , No content for selected period." }
    
          format.html # new.html.erb
          format.xml  { render :xml => @tasks }
        end
      end
    end
    
  2. 我的模特:

    class Task < ActiveRecord::Base
    
      def self.today
        Task.where(:due_date => "Date.today" , :task_status => "open").order("due_date ASC")
      end
    
      def self.this_week
        Task.where(:due_date =>"Time.now.this_week" , :task_status => "open" ).order("due_date ASC")
      end
    end
    
  3. 为什么它不在相对视图中显示任何内容。请帮帮我。

    由于

1 个答案:

答案 0 :(得分:0)

执行Task.where(...)时,您将字符串传递给:due_date,而不是日期。删除引号:

Task.where(:due_date => Date.today, :task_status => "open").order("due_date ASC")

对于你的第二个,没有名为Time.now.this_week的方法。您需要提供between范围:

Task.where("due_date between ? and ?", Date.today.beginning_of_week, Date.today.end_of_week").where(:task_status => "open").order("due_date ASC")