Assign actual days to columns in the database

Hi all, I am a newbie and need assistance from someone helpful. I have a
database which has 8 columns one of them being id the rest being:

bus_times table:

monday
tuesday
wednesday
thursday
friday
saturday
sunday

These columns have times in them which hold data for bus times. I need
something which allows me to assign each column which the day of the
week so like today is tuesday so in my view I have something which shows
tuesdays times.

What would be great however is if I can use something like Time.now or
something to actually pull the closet time a bus is avaliable so like
time now is 10.59 it will look in the table and get 11.05 as the next
time available. This is for my community and i want to help people to
show them the next avaliable bus etc via website, but I just dont know
how to do this I have being googling frantically and I have created
bus_times using rails scaffold. Some one out please can you help me
achieve this goal for my community it would be greatly appreciated thank
you.

John M. wrote:

Hi all, I am a newbie and need assistance from someone helpful. I have a
database which has 8 columns one of them being id the rest being:

bus_times table:

monday
tuesday
wednesday
thursday
friday
saturday
sunday

These columns have times in them which hold data for bus times. I need
something which allows me to assign each column which the day of the
week so like today is tuesday so in my view I have something which shows
tuesdays times.

Well, it would be easy enough to test the current date and display the
appropriate column. But what you really should do is fix your schema.
Your table should have 3 columns, not 8 – id, time, and weekday. Then
your table will be better normalized, and you can use find_by_weekday to
make this ridiculously easy.

What would be great however is if I can use something like Time.now or
something to actually pull the closet time a bus is avaliable so like
time now is 10.59 it will look in the table and get 11.05 as the next
time available. This is for my community and i want to help people to
show them the next avaliable bus etc via website, but I just dont know
how to do this

Order by time. What’s the problem?

I have being googling frantically and I have created
bus_times using rails scaffold. Some one out please can you help me
achieve this goal for my community it would be greatly appreciated thank
you.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I have norm the db but how does rails know what day it is if weekday is
a string. How do i overcome this trying hard to make it work

John M. wrote:

I have norm the db but how does rails know what day it is if weekday is
a string.

Who said it had to be a string?

Anyway, even if it is, you could use a hash or array to map one to the
other – or just use Date.format !

How do i overcome this trying hard to make it work

Think! You’re asking extremely simple questions that you should be able
to answer with a quick look in the docs.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

sorry to ask these simple questions, they are quite hard for me as I am
a newbie but thanks for your help I will just have to find a way…

I am currently trying to do something like this on my show page

def show
@bus_time = BusTime.find(:first, :order => Time.now, :conditions =>
[“time > ?”, Time.now.strftime("%H:%M:%S")])

Please note that I cant :order by Time.now but I want weekday to be
picked up so it recognises that today is tuesday weekday or tomorrow is
wednesday weekday

In my show controller i have got:

def show

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

=> [“time > ?”, Time.now.strftime("%H:%M")])

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

=> [“weekday > ?”, Time.now.strftime("%A")])

but this fails it says Mysql::Error: Unknown column ‘Tuesday’ in ‘where
clause’: SELECT * FROM bus_times WHERE (Tuesday) ORDER BY weekday
LIMIT 1

Please help

John M. wrote:

In my show controller i have got:

Why do you have two queries setting the same variable? That’s
unnecessary.

def show

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

=> [“time > ?”, Time.now.strftime(“%H:%M”)])

You don’t need strftime here. Rails knows how to turn Time objects into
query strings.

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

=> [“weekday > ?”, Time.now.strftime(“%A”)])

Your :conditions are probably wrong here. Take another look at them.
And your :order is probably wrong too.

but this fails it says Mysql::Error: Unknown column ‘Tuesday’ in ‘where
clause’: SELECT * FROM bus_times WHERE (Tuesday) ORDER BY weekday
LIMIT 1

Please help

That error cannot possibly come from either of the two find statements
you posted.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]