# Rails 1.2.2 and ranges in AR conditions clause

**URL:** <https://www.ruby-forum.com/t/rails-1-2-2-and-ranges-in-ar-conditions-clause/91973>\
**Category:** Rails\
**Created:** [April 13, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/rails-1-2-2-and-ranges-in-ar-conditions-clause/91973 "2007-04-13T22:21:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Howland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7993a0/32.png) [@Ed\_Howland](https://www.ruby-forum.com/u/Ed_Howland)\
**Post date:** [April 13, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/rails-1-2-2-and-ranges-in-ar-conditions-clause/91973/1 "2007-04-13T22:21:20Z")

</div>

Hi,

According to Jamis’ blog post:

> **[Rails 1.2.2: SQLite3, gems, singular resources](https://rubyonrails.org/2007/2/6/rails-1-2-2-sqlite3-gems-singular-resources)**
>
> It’s time for another minor update to Rails 1.2. This was primarily prompted by a change in the API for SQLite between version 3.3.7 and 3.3.8+, which left the Rails database adapter for dead by the road side. But with this release and Jamis Buck’s...

You can do this in RoR 1.2.2:  
Student.find(:all, :conditions =\> { :grade =\> 9…12 })

which automatically get converted into:  
SELECT \* FROM students WHERE grade BETWEEN 9 AND 12

but when I try this:

Customer.find :all, :conditions =\> [:id =\> 1…2] # loaded from  
fixture customer.yml

I get:

NoMethodError: undefined method `%' for {:id=>1..2}:Hash from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1419:in `sanitize\_sql\_array’  
from  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:1388:in  
`sanitize_sql' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1164:in `add\_conditions!’  
from  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:1097:in  
`construct_finder_sql' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:997:in `find\_every’  
from  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:418:in  
`find’  
from (irb):2

Can anyone help?

Thanks  
Ed

–  
Ed Howland

> **[Green Programming](http://greenprogrammer.blogspot.com)**
>
> Environmentally safe code

---

<div class="post-metadata">

**Author:** ![Ed\_Howland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7993a0/32.png) [@Ed\_Howland](https://www.ruby-forum.com/u/Ed_Howland)\
**Post date:** [April 13, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/rails-1-2-2-and-ranges-in-ar-conditions-clause/91973/2 "2007-04-13T22:30:21Z")

</div>

Hi Ed,

Ed Howland wrote:

> Student.find(:all, :conditions =\> { :grade =\> 9…12 })

> but when I try this:

> Customer.find :all, :conditions =\> [:id =\> 1…2]

> I get:
> 
> NoMethodError: undefined method `%’ for {:id=\>1…2}:Hash

I think your problem is in the use of [] instead of {} as in Jamis’  
blog.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Ed\_Howland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7993a0/32.png) [@Ed\_Howland](https://www.ruby-forum.com/u/Ed_Howland)\
**Post date:** [April 13, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/rails-1-2-2-and-ranges-in-ar-conditions-clause/91973/3 "2007-04-13T22:39:22Z")

</div>

On 4/13/07, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Ed,
> 
> Ed Howland wrote:  
> I think your problem is in the use of [] instead of {} as in Jamis’ blog.
> 
> hth,  
> Bill

Duh!  
Thanks, Bill.

That was it. I guess I was just so used to using [] for the conditions  
value.

## Ed

Ed Howland

> **[Green Programming](http://greenprogrammer.blogspot.com)**
>
> Environmentally safe code

---

<div class="post-metadata">

**Author:** ![Ed\_Howland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7993a0/32.png) [@Ed\_Howland](https://www.ruby-forum.com/u/Ed_Howland)\
**Post date:** [April 14, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/rails-1-2-2-and-ranges-in-ar-conditions-clause/91973/4 "2007-04-14T15:09:27Z")

</div>

you could also do

Customer.find((1…2).to\_a)

which is shorter in code, but could possibly cause problems if you  
have a large range

Customer.find((1…2).to\_a)  
SELECT \* FROM stories WHERE (stories.“id” IN (1,2,3,4,5))

Customer.find(:all, :conditions =\> { :id =\> (1…2) })  
SELECT \* FROM stories WHERE (stories.“id” BETWEEN 1 AND 5)

---

<div class="post-metadata">

**Author:** ![Ed\_Howland](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7993a0/32.png) [@Ed\_Howland](https://www.ruby-forum.com/u/Ed_Howland)\
**Post date:** [April 16, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/rails-1-2-2-and-ranges-in-ar-conditions-clause/91973/5 "2007-04-16T23:58:50Z")

</div>

On 4/14/07, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Customer.find(:all, :conditions =\> { :id =\> (1…2) })  
> SELECT \* FROM stories WHERE (stories.“id” BETWEEN 1 AND 5)

Chris,

Thanks, but I am actually trying to use this for Date ranges.

(Time.local(2007, 3, 1)…Time.local(2007, 4, 1)).to\_a takes a REALLY  
LONG TIME to complete. Probably because it is generating every second?

I have another question. Given :  
:conditions =\> {:created\_on =\> Time.local(2007, 3, 1)…Time.local(2007,  
4, 1)}

how can I combine this with other conditions in the query? Normally, I  
can manipulate the elements of the array (append " and " + new clause  
to conditions[0] and append new value to conditions). But given that I  
must supply a hash here to #find, I don’t see how to combine any other  
conditions.

Ed

–  
Ed Howland

> **[Green Programming](http://greenprogrammer.blogspot.com)**
>
> Environmentally safe code
