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, :conditions => { :created_at => Date.today }
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since “created_at” column contains date and time, but you need compare only date, you have two ways (I assume you use MySQL) :
scope :today, lambda { WHERE("created_at BETWEEN '#{DateTime.now.beginning_of_day}' AND '#{DateTime.now.end_of_day}'") }scope :today, lambda { WHERE('DATE(created_at) = ?', Date.today)}also, you can add “created_on” column to the table with date only.
Updated:
scope :today, lambda { where(created_on: Date.today) }