I’m using a simple Sinatra web app with a DataMapper MySql db. I declared a Datamapper Post class with property
property :created_at, DateTime
(initialized with Time.now) and now want to get the count of all posts from the last 7 days. My queries
lastweek_posts = Post.count(:created_at.gte => 1.week.ago)
lastweek_posts = Post.count(:conditions => ["created_at >= ?", Time.now.utc - 1.week])
however all return a count of 0 (database contains several entries with a created_at of today). What would be the correct query?
Many thanks in advance!
Update:
Post.all(:created_at.gte => 1.week.ago).length
returns the correct value, this is however a bad alternative as it would fetch all posts from my remote sql database.
Ok, I found the error, I simply missed to require
dm-aggregatesin my Sinatra script as clearly stated in the docs under ‘Counting’.I’m sorry for this one!