title: Show results between :year field in Rails 3
body: <p>i haved a Cars scaffold with a field year type string. Im interesting to select two years between two select_box and display the Cars between the selected years.</p>
  
  <p>I started testing in console with:</p>
  
  <pre><code>Car.maximum(:year)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Car.minimum(:year)
  </code></pre>
  
  <p>But when i starting testing with: </p>
  
  <pre><code>Car.where((self.maximum(:year))..(self.minimum(:year)))
  </code></pre>
  
  <p>I lost the head, any suggestions are welcome, thank you!</p>
  
  <p><strong>THE SOLUTION</strong></p>
  
  <p>In the model cars.rb add a method:</p>
  
  <pre><code>def self.year(fyear,tyear)
    result = order('new DESC')
    result = where(:year =&gt; fyear..tyear) if fyear.present? &amp;&amp; tyear.present? 
    result
  end
  </code></pre>
  
  <p>then in cars_controller.rb add to the index method:</p>
  
  <pre><code>@cars = Car.year(params[:fyear],params[:tyear])
  </code></pre>
  
  <p>Finally, on the view index.html.erb add the form you select the two year fields:</p>
  
  <pre><code>&lt;%= form_for cars_path, :method =&gt; :get do %&gt;
    &lt;label&gt;Mínimo&lt;/label&gt;
    &lt;%= select_tag :fyear, options_for_select(Car.all.map &amp;:year) %&gt;
    &lt;br /&gt;
    &lt;label&gt;Máximo&lt;/label&gt;
    &lt;%= select_tag :tyear, options_for_select(Car.all.map &amp;:year) %&gt;
    &lt;br /&gt;
    &lt;%= submit_tag "Buscar", :name =&gt; nil %&gt;
  &lt;/form&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>thank you beck03076, user2564200 and Abimael Martell</p>