Modify date in where clause - ruby-on-rails

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.

Related

Ruby on Rails- Time Condition

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)

Display all the records when due date is less than current date in Rails

I am having a Quote model which consists of due_date and other fields.
I need to write a condition where we have to display all the records where due_date (which is date data type and stored in database as yyyy-mm-dd format) is less than current date.
For example, current_date is today's date, so I need to display all the records till yesterday.
I wrote a condition but didn't work, can you tell me where I went wrong?
def past_quotes
#items = []
current_date = Time.now.strftime("%Y-%m-%d")
daily_items = Quote.by_account(#account).active.includes(:company).where('due_date' < current_date)
#items << Item.new(items: daily_items) unless daily_items.empty?
end
But still displaying all the records, I think condition is wrong.
You shouldn't use a string when comparing the dates. ActiveRecord should be able to handle dates directly
daily_items = Quote.by_account(#account).active.includes(:company).where('due_date < ?', Date.today)
Can you try following query:
def past_quotes
#items = []
daily_items = Quote.by_account(#account).active.includes(:company).where('due_date < DATE(?)', Time.now)
#items << Item.new(items: daily_items) unless daily_items.empty?
end

Repeat records based on date - Rails

I have a table of events that have start and end dates.
Now I want to fetch events for particular dates. i.e if I supply 1 May 2014, 2 May 2004, I need all events happening on that day (based on start and end dates). And I need to group the events based on the start date and display them as a list.
Now problem is if an event takes place on two days, say 1 May and 2 May, I need to display the event twice in the list like so:
1 May 2014
EventName
2 May 2014
EventName
I am not sure how to do this... Can anyone help me come up with an efficient way to do this?
To get the events to appear twice I would add separately created collections together.
events = []
(start_date..end_date).each do |date|
events += Event.where("start_date >= ? and end_date <= ?", date, date)
end
events
events = Event.where("start_date >= ? AND end_date <= ?", start_date, end_date)
display_events = Hash.new
(start_date..end_date).each do |date|
display_events[date] << events.select { |event| event.start_date <= date and event.end_date >= date }
end
Here you end up having a hash with events grouped by date. (I'm on cellphone, so I couldn't test it)
Try to fetch and display all events on each day.i.e iterate data not on the basis on event rather iterate on the basis of event dates.
s_date = Date.parse('2010-09-29')
e_date = Date.parse('2010-09-30')
sd.upto(ed) do |date|
#events = Event.event_on_date(date)
end

Rails Date Query for setting Expiration

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

RoR | Range of Dates not traversing multiple months

I'm trying to create an array of dates for a calendar where there are a few extra day for the next and previous month that will fill in the week.
Here is my current method to try and get the array
def calendar
selected_month = Date.civil((Time.zone.now.year).to_i, (Time.zone.now.month).to_i)
start_date = selected_month.beginning_of_month
start_date.sunday? ? start_date : start_date.beginning_of_week.advance(:days => -1)
end_date = selected_month.end_of_month
end_date.sunday? ? end_date.advance(:days => 1).end_of_week : end_date
#only puts 1-30/31 and does not include the extra off set of days from start and end. :(
date_range = (start_date..end_date).to_a
end
The problem is the rang only start at 1 and goes to the end of the month even though the start and end days exceed that.
I'm not married to this way of getting the array so maybe you have a better whole idea?
You forgot to reassign the values of start_date and end_date.
start_date = selected_month.beginning_of_month
start_date = start_date.sunday? ? start_date : start_date.beginning_of_week.advance(:days => -1)
end_date = selected_month.end_of_month
end_date = end_date.sunday? ? end_date.advance(:days => 1).end_of_week : end_date

Resources