views:

25

answers:

2

HI

i would like to write a method that returns all the events that are on tomorrow or within the next 24 hours.

e.g

def tomorrows_events
  @events = Event.will_occur_in next_24_hours
end

i have a datetime for each event which is called so to get it it would be, @event.date_and_time

i have the search logic gem installed but don't know if it supports dates, i couldn't find anything on it.

what would be the best way to write it? is there something in search logic i can use?

thanks

+2  A: 

Event.all(:conditions => { :date_and_time => (Time.now.tomorrow.beginning_of_day)..Time.now.tomorrow.end_of_day})

jpartogi
yep, use those methods in ActiveSupport
Jed Schneider
do i not need to even point it to the 'date_and_time' attribute of the event? sorry but can you please explain your code and what everything does, im new to rails and this would be a good learning experience.really appreciate it
Mo
Mo, how does the Events table looks like? How would you check the events that will occur in the next 24h if there are no fields in the table for that?
jpartogi
the events table has a datetime field called 'date_and_time'. thats why i asked you to explain the code, because your method dosent reference that field at all.
Mo
I've updated it now. Sorry for that.
jpartogi
makes so much more sense now :) thanks @jpartogi
Mo
+1  A: 

I wrote a plugin that'll help you do this: http://github.com/radar/by_star.

You'd use this command for it:

Event.tomorrow(Time.zone.now, :field => "date_and_time")

Then it will return only results for tomorrow.

Ryan Bigg
seems like a great plug-in, how do i install it?
Mo
Ryan Bigg