RoR | Range of Dates not traversing multiple months - ruby-on-rails

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

Related

upcoming 3 day from today b/w two dates rails currently it calculate only for today

def self.leave_day
self.where("? BETWEEN start_date AND end_date", Date.today).where(status: "Approved").count
end
but i want to calculate from today to next 3 days .Now it is calculate b/w two dates where status is today if give range of dates (Date.today +1)..(Date.today + 3.days) it is not accept
What do you mean with a range is not accepted? U could do it with a range and set the start_date to the beginning of the date and the end_date to the end of the day.
def self.leave_day
self.where(start_date: (Date.today.beginning_of_day)..((Date.today + 3.days).end_of_day), status: 'Approved').count
end
It's possible to use multiple params on where.
I think it should be like this :
def self.leave_day
self.where("start_date >= ? AND end_date <= ?", Date.today, Date.today + 3.days).where(status: "Approved").count
end
Source : https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-where

How to list details according to date

I have a properties tables with fields name,price, start_date, end_date.
i want to list properties between start_date and end_date.
I wrote query like this, but i am not getting correct output.
I want to convert date format like 02/12/2015 to 2015/12/02(as it is in database format).
#properties = Property.where("properties.start_date >= ? AND properties.end_date <= ? ",params[:start_date], params[:end_date]).order(updated_at: :desc)
How to list by date?
Any help is appreciatable
You should parse dates first to get them in correct format. Try something like this:
start_date = Date.parse(params[:start_date])
end_date = Date.parse(params[:end_date])
#properties = Property.where("properties.start_date >= ? AND properties.end_date <= ? ",start_date, end_date).order(updated_at: :desc)

Finding records between date range

I'm trying to find records whose start and end date range over a particular date. Date is random and :start_date and :end_date are attributes of the prices entity.
date = Time.now
record_i_want = Price.where(date => :start_date .. :end_date)
Thank you.
You can simply do
Price.where(:date => start_date..end_date)
This will result in the following SQL( for start and end dates - '2014-03-27', '2014-03-28')
SELECT `prices`.* FROM `prices` WHERE (`prices`.`date` BETWEEN '2014-03-27' AND '2014-03-28')
EDIT:
Realized that this is the query you are looking for. Thanks, Coenwulf for pointing it out
Price.where(['start_date < ? AND end_date > ?', date, date])
You want to select rows where your date is greater than the start_date and less than the end_date. You can specify the appropriate SQL where clause parameterized in your call to where like so:
Price.where([":date >= start_date AND :date <= end_date", {date: Date.today})
That will give you all the prices that match. If you know you'll get only one you can get it by calling first.
Price.where([":date >= start_date AND :date <= end_date", {date: Date.today}).first
Make any appropriate adjustment to the >= and <= if you want to exclude the start_date and/or the end_date from the results. If for example the Price is valid starting on the start_date but isn't valid through the end_date you can change the clause to:
":date >= start_date AND :date < end_date"
This should work:
def get_record_by_date(date)
Price.where([start_date.to_i < date.to_i] && [end_date.to_i > date.to_i])
end

Modify date in where clause

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.

Output array based on date (sales figures by day)

I have a table with a float called 'cost' and timestamp called'created_at'.
I would like it to output an array with the summing the costs for each particular day in the last month.
Something like:
#newarray = [] #creating the new array
month = Date.today.month # Current Month
year = Date.today.year # Current Year
counter = 1 # First Day of month
31.times do #for each day of the month (max 31)
#adding sales figures for that day
#newarray.push(Order.sum(:cost, :conditions => {:created_at => "#{year}-#{month}-#{counter}"}))
counter = counter + 1 #go onto next day
end
However this doesn't work as all the timestamps have a time as well.
Apologies in advance for the poor title, I can't seem to think of a sensible one.
You should be able to use code like the following:
sales_by_day = Order.sum(:cost,
:group => 'DATE(created_at)',
:conditions => ['DATE(created_at) > ?', 31.days.ago])
(0..30).collect { |d| sales_by_day[d.days.ago.to_date.to_s] || 0 }.reverse
This will sum the order cost by day, then create an array with index 0 being the last 24 hours. It will also take only one query, whereas your example would take 31.
To change the cut off date, replace 31.days.ago with an instance of the Time class. Documentation here: http://ruby-doc.org/core/classes/Time.html
Good luck!
This should work:
(Date.today.beginning_of_month..Date.today.end_of_month).map { |d|
Order.sum(:cost, :conditions => ['created_at >= ? AND created_at < ?', d, d+1])
}
Although I think you should try getting it using a single query, instead of making one for each day.

Resources