If I have a meet
Model in Rails,now I want to get the record yesterday,how should I do?
Meet.where(:created_at=> ) # what should I write here?
Or ActiveRecord comes with some method to filter the yesterday record?
Meet.where("DATE(created_at) = ?", Date.today-1)
Here is a slightly more generic approach, using Arel and scopes:
scope :created_since, (
lambda do |time|
where(Meet.arel_table[:created_at].gteq(time))
end
)
Meet.created_since(1.day.ago).count
If you are using Rails 5.1 or later you can use all_day
:
Meet.where(created_at: 1.day.ago.all_day)
also.. I suggest to define a scope like:
scope :yesterday, -> { where(created_at: 1.day.ago.all_day) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With