# Small doubt HELP me

**URL:** <https://www.ruby-forum.com/t/small-doubt-help-me/159415>\
**Category:** Rails\
**Created:** [February 4, 2009, 11:42am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415 "2009-02-04T11:42:42Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 4, 2009, 11:42am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/1 "2009-02-04T11:42:42Z")

</div>

Hi ,

```
I have table like this
 ---------------------------------------------------------------
|id | what | when | color | to | description |
---------------------------------------------------------------
|1 | test |2009-02-01 | red |2009-02-15 | yyyyy |
---------------------------------------------------------------
|2 | test2 |2009-02-02 | green |2009-02-14 | xxxxx |
-----------------------------------------------------------------

```

i want output like this,

2009-02-01=\>\<Div id='1’style=‘color:red’\>yyyyy  
2009-02-02=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-03=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-04=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-05=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-06=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-07=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-08=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-09=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-10=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-11=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-12=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-13=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-14=\>\<Div id='1’style=‘color:red’\>yyyyy\<Div  
id='2’style=‘color:green’\>xxxxx  
2009-02-15=\>\<Div id='1’style=‘color:red’\>yyyyy

## Note:

The ‘id’ of div is the value of ‘id’ field in database  
The ‘color’ of div is the value of ‘color’ field in database  
The ‘description’ of div is the value of ‘description’ field in database

can any one help in this for me…

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 4, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/2 "2009-02-04T12:40:42Z")

</div>

Please do a tutorial on rails

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 04/02/2009, at 9:42 PM, Shankar G.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 8:00am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/3 "2009-02-05T08:00:43Z")

</div>

this is really basic, basic, basic stuff. you should really read up on  
your basics instead of asking people to code for you.

anyway, in your controller get all the entries you want to desplay:  
@entries = Entry.all

then in your view iterate through all entries:  
\<% for entry in @entries do %\>  
\<Div id=’\<%= entry.id %\>'style=‘color:\<%= entry.color %\>’\>\<%=  
entry.when %\>  
\<% end %\>

of course you could use partials as well. but as you obviously don’t  
know much about rails, I won’t go into that here. seriously learn some  
basics.

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 8:07am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/4 "2009-02-05T08:07:09Z")

</div>

Hello,

```
This is not as regular we do for all tables.Look at the table you 

```

can see two fields ‘when’ and ‘to’. What I need is

#1 must find the range of dates between ‘when’ and ‘to’ fields,  
#2 Then I must fill the value for each date with the corresponding  
values.  
#3 Should make an array with key as date and div content as value.

Hope you got It.Anyhow thanks for reply.

cheers,  
shankar.

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 8:27am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/5 "2009-02-05T08:27:00Z")

</div>

Can you explain with a bit of code, It will be helpful for me apologizes  
I’m newbie to ROR and I’m a PHP developer.I would like to develop  
calendar using ROR when I am ti display the events I got struck here.

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 8:22am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/6 "2009-02-05T08:22:24Z")

</div>

#1  
well, if you just go like this:  
entry.when - entry.to  
you’ll get the seconds in between both times. you can then calculate  
minutes, hours, days, …

#2  
filling in the values should then be a piece of cake.

#3  
what you are looking for is not an array, but a hash. shouldn’t be  
problem, once you’ve figured out how to write some ruby-code.

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 9:09am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/7 "2009-02-05T09:09:52Z")

</div>

i’m still not sure, what exactly you are trying to do, but try  
something like this:

# create hash

hash = Hash.new

# iterate through your entries

for entry in @entries do  
# create date  
date = entry.when.to\_date  
# calculate range in days  
days = ((entry.to - entry.when)/86400).round)  
# for every day add a value to hash  
days.times do  
# build the string  
string = “

” +  
entry.description + “
”  
if hash.has\_key?(date)  
# append string  
string = hash[date] + string  
end  
# add to hash  
hash.merge!({date =\> string})  
# increase date  
date = date.advance(:days =\> 1)  
end  
end
# iterate through your hash

hash.each\_pair do |key, value|  
# output  
puts key + ‘=\>’ + value  
end

i should mention a few things:

1. i did this because i was bored. for the future, try it on your own  
and ask if you are stuck. post your code, so people can spot the error  
for you. don’t expect people to code for you.
2. i did not test the code above. i just hacked it into this post.  
there may be errors in it.
3. your page will not work with all the divs having the same id. id  
should be unique, but as php-coder you should already know that…

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 6, 2009, 5:08am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/8 "2009-02-06T05:08:24Z")

</div>

You don’t need a do when you’re using for in

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 6, 2009, 5:11am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/9 "2009-02-06T05:11:55Z")

</div>

Arrays do not have keys and values. Ruby is not php. An orderedhash is  
like a php associative array.

Your description of your problem is not clear enough. Ask simple clear  
questions and we can help then. You seem to be having algorhithm  
problems, not language problems. Explain your algorhithm to us on  
pseudocode and show the ruby you’ve tried and then we will help where  
you’re stuck.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 05/02/2009, at 6:07 PM, Shankar G.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 5, 2009, 9:45am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/10 "2009-02-05T09:45:46Z")

</div>

Ya thanks for your post, hope it will be helpful.

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 6, 2009, 5:13am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/11 "2009-02-06T05:13:06Z")

</div>

Nah, he has string fields

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 6, 2009, 5:13am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/12 "2009-02-06T05:13:41Z")

</div>

Your English Has confusions!

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 05/02/2009, at 6:27 PM, Shankar G.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 6, 2009, 5:17am UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/13 "2009-02-06T05:17:47Z")

</div>

For…in doesn’t take do!

Obviously you’re not testing your code before you suggest it!!!

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 6, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/14 "2009-02-06T13:12:36Z")

</div>

You’re right I skim them. Sorry about that. Didn’t realize you could  
use do there. It’s definitely optional tho. I fail 🙂 thanks, I learn  
something.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![lookshankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7bcc69/32.png) [@lookshankar](https://www.ruby-forum.com/u/lookshankar)\
**Post date:** [February 6, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/small-doubt-help-me/159415/15 "2009-02-06T12:59:40Z")

</div>

hallo julian,

you already said the following:

> You don’t need a do when you’re using for in  
> you’re right on that one, but you don’t have to tell me twice:  
> For…in doesn’t take do!  
> especially as this is just wrong. try it yourself:  
> for number in [1, 2, 3] do  
> puts number  
> end

> Obviously you’re not testing your code before you suggest it!!!  
> i already said that:  
> i should mention a few things:  
> 2) i did not test the code above. i just hacked it into this post.  
> there may be errors in it.  
> so: obviously you’re not reading a post before you comment on it!
