<?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>Trim Agency &#187; Rails</title>
	<atom:link href="http://old.trimagency.com/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://old.trimagency.com</link>
	<description></description>
	<lastBuildDate>Mon, 29 Jun 2026 06:32:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Active Record Queries: Specifying Conditions on Associations</title>
		<link>http://old.trimagency.com/active-record-queries-specifying-conditions-on-associations/</link>
		<comments>http://old.trimagency.com/active-record-queries-specifying-conditions-on-associations/#comments</comments>
		<pubDate>Fri, 20 Oct 2017 20:00:14 +0000</pubDate>
		<dc:creator><![CDATA[Steph Ricardo]]></dc:creator>
				<category><![CDATA[Today I Learned]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.trimagency.com/?p=2713</guid>
		<description><![CDATA[<p>I ran into a ‘gotcha’ this week while setting up an active record query that already had an eager loaded association. My goal was to only find objects that had an appointment time from the current time until the end of the day. In this case, appointments were the association and the objects were already [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/active-record-queries-specifying-conditions-on-associations/">Active Record Queries: Specifying Conditions on Associations</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>I ran into a ‘gotcha’ this week while setting up an active record query that already had an eager loaded association.</p>
<p>My goal was to only find objects that had an appointment time from the current time until the end of the day. In this case, appointments were the association and the objects were already set up to display in descending order.</p>
<pre class="brush: ruby; title: ; notranslate">
Object.where(status: status)
      .includes(:appointments)
      .order('appointments.scheduled_datetime DESC')
</pre>
<p>The objects also needed to have a status of <strong>‘scheduled’</strong>, which was already set to the variable <code>status</code> at the start of the controller action.</p>
<p>Using array conditions in my <code>where</code> statement, I started with the following:</p>
<pre class="brush: ruby; title: ; notranslate">
Object.where(status: status)
      .includes(:appointments)
      .where('scheduled_datetime &gt;= ? AND scheduled_datetime &lt;= ?',             
        current_time, end_of_day)
      .order('appointments.scheduled_datetime DESC')
</pre>
<p>I had set the <code>current_time</code> and the <code>end_of_day</code> time to variables. The string making up the array condition looked good. Yet the above resulted in the following error:</p>
<pre class="brush: bash; title: ; notranslate">
PG::UndefinedTable: ERROR:  missing 
FROM-clause entry for table “appointments”
</pre>
<p>I realized that a <code>references</code> was needed for the conditions mentioned in the <code>where</code> statement. Using the example above doesn’t join the <code>appointments</code> table.</p>
<pre class="brush: ruby; title: ; notranslate">
Object.where(status: status)
      .includes(:appointments)
      .where('scheduled_datetime &gt;= ? AND scheduled_datetime &lt;= ?',             
        current_time, end_of_day)
      .references(:appointments)
      .order('appointments.scheduled_datetime DESC')
</pre>
<p>Using <code>references</code> allows the query to know that the string in the <code>where</code> statement references appointments. It joins the appointments table to the query.</p>
<p>You may be reading this and wondering, <em>“Couldn’t you also use <code>joins</code> here?”</em>. You are right, you can use <code>joins</code> here, it would look something like this:</p>
<pre class="brush: ruby; title: ; notranslate">
Object.where(status: status)
      .joins(:appointments)
      .where(
        'appointments.scheduled_datetime &gt;= ? AND ' + 
          'appointments.scheduled_datetime &lt;= ?',             
        current_time, end_of_day
       )
</pre>
<p>This looks cleaner, yet I also want to use <code>order</code> here. You cannot simply use <code>order</code> and <code>joins</code> when ordering on associations. You would need to use <code>merge</code> like this:</p>
<pre class="brush: ruby; title: ; notranslate">
Object.where(status: status)
      .joins(:appointments)
      .where(
        'appointments.scheduled_datetime &gt;= ? AND ' + 
          'appointments.scheduled_datetime &lt;= ?',             
         current_time, end_of_day
      ).merge(Appointment.order(scheduled_datetime: :desc))
</pre>
<p>There you have it! If you are using <code>includes</code> in queries don’t forget the <code>references</code>!</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/active-record-queries-specifying-conditions-on-associations/">Active Record Queries: Specifying Conditions on Associations</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://old.trimagency.com/active-record-queries-specifying-conditions-on-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
