Reputation: 2089
How to achieve below query in ActiveRecord ?
"delete from model where date(created_at)=#{some_date}"
where created_at is sql datetime field.
One option I can think is calculate
start_date = starting time of the day
end_date = end time of the day
and
Model.delete_all('created_at' >= start_date, 'created_at' < end_date)
Any other clean option ?
Upvotes: 0
Views: 36
Reputation: 47462
Assuming database you are using is MySql and start_date is a date object.
Use mysql DATE_FORMAT
function
Model.delete_all("DATE_FORMAT(created_at, '%Y-%m-%d') >= ?",
start_date.strftime("%Y-%m-%d"))
Upvotes: 1