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 に答える