Howto: Model scope for todays records

后端 未结 5 1355
后悔当初
后悔当初 2021-02-08 05:03

Hey, how do I set a scope in rails 3 to todays records?

This doent work, yet. I get no data.

class MyModel < ActiveRecord::Base
    scope :today, :con         


        
5条回答
  •  死守一世寂寞
    2021-02-08 05:29

    Rails evaluates the scope at the class level so when you use :conditions => { :created_at => Date.today } it evaluates Date.today and compare all records with pre evaluated date. To avoid this use lamda to define date or time specific scopes

     scope :today, lambda { :conditions =>[ "created_at = ? ", Date.today] }
    

提交回复
热议问题