Topic: date find conditions not working

Hi

I'm new to Ruby and can't get this rails model method working. If anyone has any suggestions I'd appreciate them.

The method is supposed to:

-return items where open date is in the past && close date is in the future. (this is working)
-not return items where open date is in the past && close date is in the past (this is not working).
-not return items where open date is in the future && close date is in the future (this is not working).

    def self.find_currently_on
     find(:all,
     :conditions => "(open_date < now()) && (close_date > now())",
     )
    end

This is the schema

      t.column :open_date,                 :date
      t.column :close_date,                :date

Re: date find conditions not working

Usually the keyword "and" is used in conditions instead of &&. I'm not sure if all/any databases support &&.

find(:all, :conditions => "open_date < now() AND close_date > now()")

Railscasts - Free Ruby on Rails Screencasts

Re: date find conditions not working

thanks, but still not working : (

Re: date find conditions not working

Take a look at the development.log file, what is the query that is generated? Just to make sure some scopes aren't messing with it or something. If the query looks correct (only comparing the open/close date), then the problem must be with the database. Try performing the query directly on the database and see if you get the expected results or not.

Railscasts - Free Ruby on Rails Screencasts

Re: date find conditions not working

Thanks for the suggestion. The development log shows that it's not doing anything; from my interpretation and I'm only beginner with SQL as well, it looks like it's returning all 'exhibitions'. This is the head of the call. All the stuff following it is fine. I don't know anything about scoping, perhaps I need to read about this? Or alternatively just use SQL for the selection in the method?

Processing ExhibitionsController#currently_on (for 127.0.0.1 at 2006-12-21 10:47:15) [GET]
  Session ID: a9b15842f8064bab869f8803deb128a7
  Parameters: {"action"=>"currently_on", "controller"=>"exhibitions"}
  Exhibition Load (0.000000)   SELECT * FROM exhibitions 
Rendering  within layouts/exhibitions
Rendering exhibitions/currently_on

Re: date find conditions not working

Are you sure the find_currently_on method is getting called? What does the "currently_on" action look like in your Exhibitions controller?

Railscasts - Free Ruby on Rails Screencasts

Re: date find conditions not working

ha ha, we're retarded. communication breakdown between programmers; no, it wasn't being called, things had been changed

thanks for pointing out the obvious, it was of much assistance!

-xia