0

我的模型上有这个

Class User
 include Mongoid::Document
  scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_date...end_time)) }

  class << self
    ## Class methods for calculating relative users

    def created_today
      today = Time.zone.now
      created_between(today.beginning_of_day, today.end_of_day)
    end

    def created_yesterday
      yesterday = Time.zone.now - 1.day
      created_between(yesterday.beginning_of_day, yesterday.end_of_day)
    end

    def created_last_week
      start_time = (Time.zone.now - 1.week).beginning_of_day
      end_time = Time.zone.now
      created_between(start_time, end_time)
    end

    def created_last_month
      start_time = (Time.zone.now - 1.month).beginning_of_day
      end_time = Time.zone.now
      created_between(start_time, end_time)
    end

    def created_last_year
      start_time = (Time.zone.now - 1.year).beginning_of_day
      end_time = Time.zone.now
      created_between(start_time, end_time)
    end
  end
 end

在我的控制器中:

 def index
   @users_created_today = User.created_today.all
   @users_created_today = User.created_today.all
   @users_created_yesterday = User.created_yesterday.all
   @users_created_last_week = User.created_last_week.all
   @users_created_last_month = User.created_last_month.all
   @users_created_last_year = User.created_last_year.all
   .
   .
   .
 end

我得到下一个错误:

undefined local variable or method `start_date' for User:Class

非常感谢你

4

1 回答 1

0

好吧,这是一个错字

 scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_date...end_time)) }

你应该使用start_time而不是start_date

于 2012-11-08T12:22:41.827 回答