<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ruby &#8211; Chris Philbin&#039;s Blog</title>
	<atom:link href="https://www.chrisphilbin.net/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.chrisphilbin.net</link>
	<description>Perfection Through Persistence.</description>
	<lastBuildDate>Thu, 04 Jun 2020 10:57:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>
	<item>
		<title>Finding today&#8217;s birthdays and upcoming birthdays with Rails</title>
		<link>https://www.chrisphilbin.net/finding-todays-birthdays-and-upcoming-birthdays-with-rails/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Thu, 04 Jun 2020 10:57:47 +0000</pubDate>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<guid isPermaLink="false">https://chrisphilbin.net/?p=234</guid>

					<description><![CDATA[Suppose you want the ability to find all current birthdays or all upcoming birthdays &#8211; how would you go about doing it? The first approach &#8211; finding records that have&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Suppose you want the ability to find all current birthdays or all upcoming birthdays &#8211; how would you go about doing it?  </p>



<p>The first approach &#8211; finding records that have today&#8217;s date as their birthday is fairly straight forward. (We&#8217;re assuming that the &#8216;records&#8217; we&#8217;re dealing with here are employee records, people records, etc&#8230; Something that would obviously have a date of birth field)  We just need to keep in mind that when we create the query, we must omit the year.  We&#8217;d accomplish this by writing the below:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
  def self.todays_birthdays
    #return array of employee objects whos birthday it is today based on MM/DD
    Employee.where(&quot;strftime(&#039;%m%d&#039;, date_of_birth) = ?&quot;, Date.today.strftime(&#039;%m%d&#039;))
  end
</pre></div>


<p>But what about when you want to show <strong>upcoming birthdays</strong>? I&#8217;m not going to lie, it did take me a little bit to figure this one out. Luckily for us, Ruby has a few nice built in methods to help us out: <strong>#days.ago</strong> and <strong>#days.from_now</strong> to the rescue:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
  def self.upcoming_birthdays
    #return array of employee objects who have an upcoming birthday within the next 14 days
    Employee.where(date_of_birth: 0.days.ago .. 14.days.from_now)
  end
</pre></div>


<p>Combine those two methods, along with the ability to create ranges ( <strong>..</strong> and <strong>&#8230; </strong>) we&#8217;re able to return an array of upcoming objects who have a birthday that falls within THE RANGE of dates we specify, which in this case is between today (o.days.ago) and 14 days from now (14.days.from_now).</p>



<p>One fallacy that developers might tend to run in to is to simply return all objects who have a birthday in a given month.  The issue with that, however, is what if we&#8217;re on the last day of the month and someone has a birthday on the first day of the next month? We&#8217;d have no viability into their birthday until after midnight when the numerical value of the month changes.  Creating a query that returns a list based on a range of days is probably the most effective methodology as it will account for that use case, as well as on the dreaded leap year as well.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
