Reputation: 1351
so I'm working on a personal project.
I'm a truck driver and i am building a simple app to track and calculate revenue ect..
What I am trying to do (and there may be a much better way to do it) is highlight what my current active fuel surcharge is based only on a range of months.
In this case its April 1 to Oct 31 and Nov 1 to March 31 this can change as contract requirements dictate ect.
I have an activeDate and endDate column in my database and this is what I have come up with (but doesn't seem to work).
**<<index.html.erb>>**
<tbody>
<% @fscs.each do |fsc| %>
<% if Date.today === (fsc.activeDate..fsc.endDate) %>
<tr>
<td class="fsc_active"><%= fsc.name %></td>
<td class="fsc_active"><%= fsc.rate %></td>
<td class="fsc_active"><%= fsc.activeDate %></td>
<td class="fsc_active"><%= fsc.endDate %></td>
<td><%= link_to 'View', fsc %></td>
<td><%= link_to 'Edit', edit_fsc_path(fsc) %></td>
<td><%= link_to 'Delete', fsc, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% else %>
<tr>
<td class="fsc_inactive"><%= fsc.name %></td>
<td class="fsc_inactive"><%= fsc.rate %></td>
<td class="fsc_inactive"><%= fsc.activeDate %></td>
<td class="fsc_inactive"><%= fsc.endDate %></td>
<td><%= link_to 'View', fsc %></td>
<td><%= link_to 'Edit', edit_fsc_path(fsc) %></td>
<td><%= link_to 'Delete', fsc, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>
</tbody>
Any advice on where I'm going wrong or pointers on how to make this better would be greatly appreciated! please let me know if more information is needed.
EDIT 1: Sorry Only the month matters in this case not the year or day. my apologies for not mentioning this.
Upvotes: 0
Views: 146
Reputation: 2404
(fsc.activeDate..fsc.endDate).cover?(Date.today)
Instead of using include?
, use cover?
. It’s substantially more efficient. include?
will create a whole array of all possible values to see if the value is in the list. cover?
just uses the min and max values with a >=
and <=
to see if the value is within the range. There are instances where cover?
is not an applicable replacement for include?
, but this is a good time to use cover?
.
https://ruby-doc.org/core-2.5.3/Range.html#method-i-cover-3F
I would also recommend that you store Date.today
in a variable right before the start of your loop and use that in your comparison, instead of checking the current time on the server for every element in @fscs
.
Also, it would be wise to use Date.current
instead of Date.today
. Date.current
is a Rails method that’s time zone aware to give you today in the currently configured Time.zone
.
Here’s an article I found helpful to learn about what methods to use and what methods to avoid, when dealing with all the fun that comes with time zones. https://thoughtbot.com/blog/its-about-time-zones
Upvotes: 3
Reputation: 121000
You still might use triple-equal aka case-equal FWIW. Just swap the arguments to #===
: you need the one declared on Range
, not the opposite one:
- Date.today === (fsc.activeDate..fsc.endDate)
+ (fsc.activeDate..fsc.endDate) === Date.today
Upvotes: 2
Reputation: 304
Try doing something like this:
(fsc.activeDate..fsc.endDate).include?(Date.today)
You are trying to equate todays date with a range of dates whereas you want to check if the today's date is present in the date range.
Upvotes: 3