Matt
Matt

Reputation: 99

Select all posts created this current week with Rails

posts_controller.rb

def index
  @current_week = Post.where(:created_at => (Date.today - 7)..(Date.today))                      
end

views/index.html.erb

@current_week.each do |post|
  ....
end

Rails return an empty array. I just want to select all the video created the current week. Do you see where I'm wrong ?

Thank you

Upvotes: 3

Views: 959

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Try at_beginning_of_week to fetch records created in the current week

@current_week = Post.where("created_at >= ?", Date.today.at_beginning_of_week)

Hope that helps!

Upvotes: 6

Related Questions