I have an admins dashboard which displays posts created in the last 24 hours, 7 days, 28 days etc.
def index @1DayPosts = Post.where(created_at: 1.days.ago..DateTime.now).count @7DaysPosts = Post.where(created_at: 7.days.ago..DateTime.now).count @28DaysPosts = Post.where(created_at: 28.days.ago..DateTime.now).count end How could I make this into one line? Something like the below:
def index @calculatePosts(a) = Post.where(created_at: a.days.ago..DateTime.now).count end Then in the view I could do:
=@calculatePosts(1) Or would I need to create a new method?
def calculatePosts(a) @calculatePost = Post.where(created_at: a.days.ago..DateTime.now).count end How would I then call this in the index view?