What if statements help with dates

Hi all, I am a newbie and just recently started to create apps with ruby on rails so please go easy on me :slight_smile:

I have an app which has a database in mysql which is called bus_times and consists of 3 columns which are: id, time and weekday. Time is set to time and weekday is a string. So it looks something like this:

1 monday 20:00:00 2 tuesday 12:00:00 3 wednesday 3:00:00 4 wednesday 15:00:00 ..etc

What I want to do is to pick out the times from my database and show them on my view depending on what the date is today. Hence assign my strings mon,tues,wed etc to an actual date so like today is wednesday I want it to pick out just the wednesday times. How do I do this I have looked everywhere and cannot find a solution thanks in advance...

I can give you an example, you would have to improvise on the same according to your needs.

@bus_times = BusTime.find(:all, :conditions => [‘weekday = ?’, Date::DAYNAMES[Date.today.wday]])

The above would give you all the bus_time records where weekday equals todays day…

Hope it helps.

Thanks & Regards, Dhruva Sagar.

Hi Dhruva,

I tried this and get: No method error You have a nil object when you didn't expect it! The error occurred while evaluating nil.weekday

I would also like to point out that in my def show I have the following:

@bus_time = BusTime.find(:first, :order => "weekday", :conditions => ["time

?", Time.now])

This helps me to get all the next available times on that day, can you see what I am trying to do, I am trying to get the latest times available for a bus on a particular day because sunday's times are different please could you help thanks for the very quick reply

Dhruva Sagar wrote:

The error implies that the find returned no records and you were trying to access it in the view to display it perhaps ?

Try the following :

@bus_time = BusTime.find(:first, :order => “weekday”, :conditions => [“time > ? AND weekday = ?”, Time.now, Date::DAYNAMES[Date.today.wday]])

If it still gives you the same error, please look at the logs and check the SQL query running in the background, if it’s returning no records see what’s wrong in the query / database.

Thanks & Regards, Dhruva Sagar.

Problem solved thank you for your help I appreciated instead of all i called :first it was listing an array thats all thanks

Dhruva Sagar wrote: