Peege151
Peege151

Reputation: 1560

Ruby Where Query, Not Picking Up Any Objects

I'm working with datetime and I'm wondering why I don't get any results from the query I'm making.

In rails c:

>> Performance.find(6)
     Performance Load (0.4ms)  SELECT  "performances".* FROM "performances"  WHERE   
    "performances"."id" = $1 LIMIT 1  [["id", 6]]

I chose the 6th one because I knew the date~ arbitray. Here is what it responds.

     => #<Performance id: 6, show: "", choreographer: "", location: "", tickets: "", created_at: 
     "2014-10-01 06:43:41", updated_at: "2014-10-01 06:43:41", has_passed: nil, event: nil, date:
     "2014-12-06 07:15:00"> 

Note the date at the end. It is in the format

2014-12-06 {...}

However, when I try and pull this record up with a where query

Performance.where(date: (Time.now.midnight + 10.year)..(Time.now.midnight - 10.year))

returns

Performance Load (0.7ms)  SELECT "performances".* FROM "performances" 
    WHERE    
("performances"."date" BETWEEN '2024-10-01 04:00:00.000000' AND '2004-10-01 04:00:00.000000')
    => #<ActiveRecord::Relation []> 

I'm not surprised that I'm doing something wrong, but what I don't get how the formating looks so similar.

I'm refering to the BETWEEN X and Y, and the format of the Performance.find(6) formatting.

How do I need to change the query to make sure that I pick up the 6th record?

Thanks

Upvotes: 0

Views: 24

Answers (1)

Mike Campbell
Mike Campbell

Reputation: 7978

You have to have the smallest date on the left, because the BETWEEN operator behaves like this:

expr BETWEEN begin_exp AND end_expr
// is the same as
expr >= begin_exp AND expr <= end_expr

There are no dates that are "greater than" than '2024-10-01 04:00:00.000000' and "less than" '2004-10-01 04:00:00.000000'.

Upvotes: 2

Related Questions