title: Rails-y way to query a model with a belongs_to association
body: <p>I have two models:</p>
  
  <pre><code>class Wine
    belongs_to :region
  end
  
  class Region
    has_many :wines
  end
  </code></pre>
  
  <p>I am attempting to use the #where method with a hash built from transforming certain elements from the params hash into a query hash, for example <code>{ :region =&gt; '2452' }</code></p>
  
  <pre><code>def index
    ...
    @wines = Wine.where(hash)
    ...
  end
  </code></pre>
  
  <p>But all I get is a column doesn't exist error when the query is executed:</p>
  
  <pre><code>ActiveRecord::StatementInvalid: PGError: ERROR:  column wines.region does not exist
  LINE 1: SELECT "wines".* FROM "wines"  WHERE "wines"."region" =...
  </code></pre>
  
  <p>Of course, the table <code>wines</code> has <code>region_id</code> so if I queried for <code>region_id</code> instead I would not get an error.</p>
  
  <p><strong>The question is the following:</strong></p>
  
  <p>Is there a rails-y way to query the <code>Wine</code> object for specific <code>regions</code> using the <code>id</code> in the <code>#where</code> method? I've listed some options below based on what I know I can do.</p>
  
  <p>Option 1:
  I could change the way that I build the query hash so that each field has <code>_id</code> (like <code>{ :region_id =&gt; '1234', :varietal_id =&gt; '1515' }</code> but not all of the associations from <code>Wine</code> are <code>belongs_to</code> and thus don't have an entry in wines for <code>_id</code>, making the logic more complicated with joins and what not.</p>
  
  <p>Option 2:
  Build a SQL where clause, again using some logic to determine whether to use the id or join against another table... again the logic would be somewhat more complicated, and delving in to SQL makes it feel less rails-y. Or I could be wrong on that front.</p>
  
  <p>Option(s) 3..n:
  Things I haven't thought about... your input goes here :)</p>