title: Rails - how to select all records by ID range
body: <p>I am trying to do a query for all cities (selecting only their name attribute) by their ID, and I want to be able to specify a range of ID's to select. My code is below:</p>
  
  <pre><code>def list_cities(start, stop)
    cities = City.all(order: 'name ASC', id: start..stop, select: 'name')
    cities.map { |city| "&lt;li&gt; #{city.name} &lt;/li&gt;" }.join.html_safe
  end
  </code></pre>
  
  <p>However, I get an error:</p>
  
  <pre><code>Unknown key: id
  </code></pre>
  
  <p>My implementation in my view is: </p>
  
  <pre><code>&lt;%= list_cities(1,22) %&gt;
  </code></pre>
  
  <p>This is a helper method to be put in all views, so I am not putting the logic in a particular controller.</p>
  
  <p>My schema for this model is:</p>
  
  <pre><code>  create_table "cities", :force =&gt; true do |t|
      t.datetime "created_at",      :null =&gt; false
      t.datetime "updated_at",      :null =&gt; false
      t.string   "neighborhoods"
      t.string   "name"
      t.integer  "neighborhood_id"
    end
  </code></pre>
  
  <p>When I ran the method in my console, I got:</p>
  
  <pre><code>City Load (0.9ms)  SELECT name FROM "cities" WHERE ("cities"."id" BETWEEN 1 AND 3) ORDER  BY name ASC
   =&gt; "" 
  </code></pre>
  
  <p>I know it's not an issue of having an empty database since it worked with the following version of the method:</p>
  
  <pre><code>def list_cities(start, stop)
    cities = City.all(order: 'name ASC', limit: stop - start, select: 'name')
    cities.map { |city| "&lt;li&gt; #{city.name} &lt;/li&gt;" }.join.html_safe
  end
  </code></pre>
  
  <p>However, this method returns only the first 'n' records and not a range like I want.</p>
  
  <hr>
  
  <p>When trying a simpler query in the console:</p>
  
  <pre><code>1.9.3p385 :009 &gt; City.where(:id =&gt; 1..4)
    City Load (0.9ms)  SELECT "cities".* FROM "cities" WHERE ("cities"."id" BETWEEN 1 AND 4)
   =&gt; [] 
  </code></pre>
  
  <p>I figured out why it was happening...</p>
  
  <p>I did City.all in my console and realized that my cities started with id "946" because I had seeded multiple times and the ID's were not what I thought they were! The solution offered was correct!</p>