title: Rails try() method with where constraint in active query
body: <p>I have the following models:</p>
  
  <p>Supplier has many submitted_prices</p>
  
  <p>Submitted_prices have many price_items</p>
  
  <p>In order for the code not to crash when there are no submitted_prices and/or no price_items, I used to use the following:</p>
  
  <pre><code> if supplier.submitted_prices.where(:purchaser_id =&gt; organization.id).last
   &amp;&amp; supplier.submitted_prices.where(:purchaser_id =&gt; organization.id).last.price_items.where("item_id" =&gt; item.id).last
   &amp;&amp; supplier.submitted_prices.where(:purchaser_id =&gt; organization.id).last.price_items.where("item_id" =&gt; item.id).last.unit_price.present?
    order_item.unit_price = supplier.submitted_prices.where(:purchaser_id =&gt; organization.id).last.price_items.where("item_id" =&gt; item.id).last.unit_price 
  </code></pre>
  
  <p>Then I came across try():</p>
  
  <pre><code>order_item.unit_price = supplier.try(:submitted_prices).where(:purchaser_id =&gt; organization.id).try(:last).try(:price_items).where("item_id" =&gt; item.id).try(:last).try(:unit_price)
  </code></pre>
  
  <p>Problem is, I don't know how to run try() on the WHERE constraints and this constraints fails if try returns nil:</p>
  
  <pre><code>NoMethodError Exception: undefined method `where' for nil:NilClass
  </code></pre>
  
  <p>Is there a way to wrap try() around the WHERE constraint?</p>