title: How do I get back an ActiveRecord instead of an ActiveRecord relation?
body: <p>Using Rails 5.  How do I get an ActiveRecord instead of an ActiveRecord relation?  I ahve the below model</p>
  
  <pre><code>class State &lt; ActiveRecord::Base
    belongs_to :country
  
      ...
    def self.cached_find_us_state_by_name(name)
      Rails.cache.fetch("#{name.upcase} US") do
        find_us_state_by_name(name)
      end
    end
  
    # Look up a US state by its full name 
    def self.find_us_state_by_name(name)
      search_criteria = ["upper(states.name) = ? "]
      search_criteria.push( "states.country_id = countries.id " ) 
      search_criteria.push( "countries.iso = 'US' " ) 
  
      results = State.joins(:country).where( search_criteria.join(' and '),
                                             name.upcase) 
    end
  </code></pre>
  
  <p>but when I lookup an item using the methods, waht I get back is an ActiveReocrd relation ...</p>
  
  <pre><code>2.4.0 :004 &gt;  State.cached_find_us_state_by_name('Ohio')
   =&gt; #&lt;ActiveRecord::Relation [#&lt;State id: 3538, name: "Ohio", country_id: 233, iso: "OH"&gt;]&gt;
  </code></pre>
  
  <p>This is cauisng problems later on, specifically ...</p>
  
  <pre><code>ActiveRecord::AssociationTypeMismatch: State(#70288305660760) expected, got State::ActiveRecord_Relation(#70288290686360)
      from /Users/davea/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/associations/association.rb:221:in `raise_on_type_mismatch!'
      from /Users/davea/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/associations/belongs_to_association.rb:12:in `replace'
  </code></pre>
  
  <p><b>Edit:</b> Per the suggestion given, I changed my methods to</p>
  
  <pre><code>  def self.cached_find_us_state_by_name(name)
      Rails.cache.fetch("#{name.upcase} US") do
        find_us_state_by_name(name)
      end
    end
  
    # Look up a US state by its full name 
    def self.find_us_state_by_name(name)
      search_criteria = ["upper(states.name) = ? "]
      search_criteria.push( "states.country_id = countries.id " ) 
      search_criteria.push( "countries.iso = 'US' " ) 
  
      results = State.joins(:country).where( search_criteria.join(' and '),
                                             name.upcase).take  
    end
  </code></pre>
  
  <p>but alas, I still get an ActiveRecord::Relation</p>
  
  <pre><code>2.4.0 :011 &gt; State.cached_find_us_state_by_name('Ohio')
   =&gt; #&lt;ActiveRecord::Relation [#&lt;State id: 3538, name: "Ohio", country_id: 233, iso: "OH"&gt;]&gt;
  </code></pre>