Forum: Ruby on Rails Rails 1.2.2 and ranges in AR conditions clause.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
822a498b26a2cb7d1f0f2e7e37ce61b2?d=identicon&s=25 Ed Howland (Guest)
on 2007-04-13 22:21
(Received via mailing list)
Hi,

According to Jamis' blog post:
http://weblog.rubyonrails.org/2007/2/6/rails-1-2-2...

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
http://greenprogrammer.blogspot.com
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-04-13 22:30
(Received via mailing list)
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
822a498b26a2cb7d1f0f2e7e37ce61b2?d=identicon&s=25 Ed Howland (Guest)
on 2007-04-13 22:39
(Received via mailing list)
On 4/13/07, Bill Walton <bill.walton@charter.net> 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
http://greenprogrammer.blogspot.com
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-04-14 15:09
(Received via mailing list)
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)
822a498b26a2cb7d1f0f2e7e37ce61b2?d=identicon&s=25 Ed Howland (Guest)
on 2007-04-16 23:58
(Received via mailing list)
On 4/14/07, Chris Hall <christopher.k.hall@gmail.com> 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
http://greenprogrammer.blogspot.com
This topic is locked and can not be replied to.