user7067399 user7067399 - 1 year ago 75
Ruby Question

select posts from one day

I want to select posts from one day. What is the correct way? This errors:

date = '3/11/2016'.to_date

User.joins(:posts).where(posts: {'created_at >= ? AND created_at <= ?', date.beginning_of_day, date.end_of_day})

Answer Source
Posts.where('created_at >= ? and created_at <= ?',,

There is no need to join User unless you want the user as well then you could do this

User.joins(:posts).all.where('posts.created_at >= ? and posts.created_at <= ?',,

The preload will help with efficiency

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download