Forum: Ruby on Rails small doubt HELP me

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shankar G. (Guest)
on 2009-02-04 12:42
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</Div>
2009-02-02=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-03=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-04=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-05=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-06=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-07=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-08=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-09=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-10=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-11=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-12=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-13=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-14=><Div id='1'style='color:red'>yyyyy</Div><Div
id='2'style='color:green'>xxxxx</Div>
2009-02-15=><Div id='1'style='color:red'>yyyyy</Div>

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......
Julian L. (Guest)
on 2009-02-04 13:40
(Received via mailing list)
Please do a tutorial on rails

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 04/02/2009, at 9:42 PM, Shankar G.
<removed_email_address@domain.invalid
MaD (Guest)
on 2009-02-05 09:00
(Received via mailing list)
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 %></Div>
  <% 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.
Shankar G. (Guest)
on 2009-02-05 09:07
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.
MaD (Guest)
on 2009-02-05 09:22
(Received via mailing list)
#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.
Shankar G. (Guest)
on 2009-02-05 09:27
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.
MaD (Guest)
on 2009-02-05 10:09
(Received via mailing list)
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 = "<Div id='1' style='color:" + entry.color + "'>" +
entry.description + "</Div>"
      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...
Shankar G. (Guest)
on 2009-02-05 10:45
Ya thanks for your post, hope it will be helpful.
Julian L. (Guest)
on 2009-02-06 06:08
(Received via mailing list)
You don't need a do when you're using for in

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
Julian L. (Guest)
on 2009-02-06 06:11
(Received via mailing list)
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/
Learn rails: http://sensei.zenunit.com/

On 05/02/2009, at 6:07 PM, Shankar G.
<removed_email_address@domain.invalid
Julian L. (Guest)
on 2009-02-06 06:13
(Received via mailing list)
Nah, he has string fields

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
Julian L. (Guest)
on 2009-02-06 06:13
(Received via mailing list)
Your English Has confusions!

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 05/02/2009, at 6:27 PM, Shankar G.
<removed_email_address@domain.invalid
Julian L. (Guest)
on 2009-02-06 06:17
(Received via mailing list)
For...in doesn't take do!

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

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
MaD (Guest)
on 2009-02-06 13:59
(Received via mailing list)
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!
Julian L. (Guest)
on 2009-02-06 14:12
(Received via mailing list)
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/
Learn rails: http://sensei.zenunit.com/
This topic is locked and can not be replied to.