# Assign actual days to columns in the database

**URL:** <https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662>\
**Category:** Rails\
**Created:** [December 1, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662 "2009-12-01T12:01:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/1 "2009-12-01T12:01:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/2 "2009-12-01T15:04:44Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 3:51pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/3 "2009-12-01T15:51:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 4:04pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/4 "2009-12-01T16:04:33Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/5 "2009-12-01T16:11:43Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/6 "2009-12-01T15:57:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/7 "2009-12-01T16:37:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 1, 2009, 5:02pm UTC](https://www.ruby-forum.com/t/assign-actual-days-to-columns-in-the-database/179662/8 "2009-12-01T17:02:45Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
