I want to compare the current time with submission with Datetime stamp to setup a controller which only shows submission of a specific time. Such as posts done in past hour, day or week.
So I don't get how to access current time. Does it need to be part of database? How can it be defined and how to compare it which post submissions?
To add to the earlier answer
For posts this week
#posts = Post.where("created_at > ?", Time.now.beginning_of_week)
For posts this month:
#posts = Post.where("created_at > ?", Time.now.beginning_of_month)
From the below query you will get the posts that got created one hours ago.
now = Time.now
#posts= Post.where(created_at: (now - 1.hours)..now)
To get posts created one week ago
#posts = Post.where("created_at > ?", Time.now - 7.days)
Related
i'm trying to create some kpi's on ruby on rails.
How can i count all users that was created on sunday or on wednesday for example?
I now that ruby can get day of week with the follow code for example:
u = User.last
u.created_at.wday #==> wday get the day of week (0: sunday, 1: monday..)
But the following doesn't work:
User.where(created_at.wday: 0).count
And I don't want to loop each user, check if it was created on sunday and put it in an array, because it seems to be costly.
Any ideas?
If you're using MYSQL you can use dayofweek
User.where('DAYOFWEEK(created_at) = ?', 0).count
For Postgres this should do the trick:
User.where("extract(dow from created_at) = ?", 0)
EDIT: For SQLITE the following works:
User.where("strftime('%w', created_at) = ?", 0)
I am trying to get the records created only today, yesterday, and the day before yesterday. My model is Visitor
For items created today, I am trying
Visitor.where(created_at: Date.today)
And then for items created yesterday, I am trying
Visitor.where(created_at: Date.yesterday)
But then it is not working because the Created_at column is in form of a timestamp, while Date.today or yesterday is in a date format. How can I make this work.
And also how can I get the items creates the day before yesterday?
#Today
Visitor.where("DATE(created_at) = ?", Date.today)
#Yesterday
Visitor.where("DATE(created_at) = ?", Date.yesterday)
Alternatively:
#Today
Visitor.where(created_at: Date.today...Date.today+1)
#Yesterday
Visitor.where(created_at: Date.yesterday...Date.today)
Ref: How can I find records from today, yesterday and so on with Ruby on Rails?
To get visitors created after a certain time, for example the last 2 days:
Visitor.where(["created_at > ?", 2.days.ago.midnight])
I want to display the hottest posts in 3 days, so this is what I do
#posts = Post.created_in_days(3).order_by_likes.first(4)
For queries:
scope :order_by_likes, -> { order('likes IS NULL, likes DESC') }
scope :created_in_days, ->(number) {where('created_at >= ?', Time.zone.now - number.days)}
This works fine. But the problem is, sometimes there is not enough posts generate in 3 days. Maybe just 3, or even no post at all.
How can I handle this so when #posts created in 3 days is not enough, it would extend the time, till it get enough #posts? (In this case, 4 post needed).
scope :created_in_days, ->(number) do
filtered = where('created_at >= ?', Time.zone.now - number.days)
if filtered.length < 4
order(:created_at => :desc).limit(4) # takes last 4 created
else
filtered
end
end
I'm creating a marketplace app where sellers can list items to sell. I want to set an expiry date so listings over 30 days old do not show on the site.
I found some similar examples online but can't get this query to work.
#listings = Listing.where('created_at <= ?', Date.created_at + 30.day)
You want to query items whose created_at time is >= the current date (Date.current) - 30 days (30.day). So the query should simply be:
#listings = Listing.where('created_at >= ?', Date.current - 30.day)
You can also replace Date.current with Time.now or DateTime.now.
UPDATE: as user3334690 mentioned in a comment, it's recommended that you make this a model method since it's something that should be in the Model layer:
# app/models/listing.rb
def self.not_expired
where('created_at >= ?', Date.current - 30.day)
end
# now in controllers you can do something like
#listings = Listing.not_expired
I'm using Rails 4
I am trying to find all bookings with an arrival date within the next 5 days. arrival is a date datatype. Here is my attempt:
#bookings = Booking.where("? > arrival-5.days", Time.now.to_date)
I've also tried:
#bookings = Booking.where("? > ?", Time.now.to_date, arrival-5.days)
but neither work. How could I get this to work?
#bookings = Booking.where(["arrival <= ?", 5.days.from_now])
This would also pull in bookings from yesterday, but I'll let you figure that one out.