Associative arrays


#1

I just started playing with rails this week and am working on porting
our
existing website to rails

in our navigation we have a list that has the days of the week that link
to
the articles for those days.

this seems to be working when I had it in the template it would give me
the
array I was looking for so I decided to move it to the helper so that I
could use it on all my templates.
Application_helper.rb

def days
t=Time.now()
#days =
[‘Monday’,‘Tuesday’,‘Wednesday’,‘Thursday’,‘Friday’,‘Saturday’,‘Sunday’]
#YY,MM,DD,h,m,s
days = Array.new
for i in 1…8
d = Time.local(t.year,t.month,t.day-i)
#n = d.to_i
today = d.strftime("%A")
days.insert(‘today’,d.to_i)
end
end

I know this is not correct but it is what I am looking to do.
when the above code was in the template minus the def these lines would
spit
out the error about converting strings to integers.

application.rhtml

  • Monday
  • Tuesday
  • Wednesday
  • Thursday
  • Friday
  • Saturday
  • Sunday
  • I am coming from about 7 years of php background so go easy on me :slight_smile:

    Thanks for your help


    #2

    Spectre013 wrote:

    in our navigation we have a list that has the days of the week that link
    to the articles for those days.

    I don’t know about your specific case, but since nobody replied, I just
    thought I’d chime in that in Ruby land we use ‘class Hash’ for
    Associative Arrays.

    HTH…

    –Steve


    #3

    Helpers are just methods so you can’t really access them this way.
    You can call days() inside your template and assign it to a variable:

    <% days = get_days() %>


    #4

    On 16-dec-2005, at 16:51, Spectre013 wrote:


            today = d.strftime("%A")
    

    I am coming from about 7 years of php background so go easy on me :slight_smile:

    Thanks for your help

    You need Hash.new (or just {} ) instead of Array.new. Array will
    complain if you feed it Strings as keys - moreover, when you set a
    key in an Array which does not exist, the interval to the last
    existing key will be filled with nils.

    For example:

    ar = [1,2,3,4,5]
    ar[7] = 3

    ar # will be [1, 2, 3, 4, 5, nil, nil, 3]

    Beware though that Ruby hashes are UN-ordered (which means that you
    can’t set months there and then iterate through them in the same order)

    myhash = {}
    hash[:key1] = 2
    hash[:key2] = 4 etc.

    but when you iterate over them with each_pair you might as well get
    key2 first.


    #5

    Spectre013 wrote:

    I just started playing with rails this week and am working on porting
    our
    existing website to rails

    in our navigation we have a list that has the days of the week that link
    to
    the articles for those days.

    this seems to be working when I had it in the template it would give me
    the
    array I was looking for so I decided to move it to the helper so that I
    could use it on all my templates.

    You must use a Hash instead of an Array in this
    case (beware, not tested):

    This should work

    def weekdays()
    @weekdays = {}
    7.times {|i|
    # I believe Rails provides Time#days, or just sub 606024*i
    d = Time.now - i.days
    @weekdays[d.strftime("%A")] = d.to_i
    }
    @weekdays
    end

    You could be fancy and use Enumerable#inject

    def weekdays2()
    # ‘inject’ an empty Hash to be filled
    (0…7).to_a.inject({}) {|hash, i|
    d = Time.now - i.days
    hash[d.strftime("%A")] = d.to_i
    hash
    }
    end

    Then you can call it as

    weekdays[‘Monday’]

    Or

    @days = weekdays
    @days[‘Tuesday’]

    I know this is not correct but it is what I am looking to do.
    when the above code was in the template minus the def these lines would
    spit
    out the error about converting strings to integers.

    The error is simply due to Arrays only accepting
    numeric indices.

    Thanks for your help

    E


    #6

    If I have a collection_select in my form, should it automatically
    update the id for the table it is populating with the foreign key? I
    don’t think it is, because the code as it is now is leaving person_id
    as nil. Any suggestions?

    in _form.rthml

    Paid by
    <%= @people = Person.find(:all ) collection_select(:person, :fullname, @people, :id, :fullname) %>

    models:

    class Bill < ActiveRecord::Base
    belongs_to :person
    end

    class Person < ActiveRecord::Base
    has_many :bills
    end

    Thanks,

    Ivan S.


    #7

    On 12/18/05, Ivan S. removed_email_address@domain.invalid wrote:

    If I have a collection_select in my form, should it automatically
    update the id for the table it is populating with the foreign key? I
    don’t think it is, because the code as it is now is leaving person_id
    as nil. Any suggestions?

    Assuming @bill is the instance variable for the Bill object, try:

    Paid by
    <%= select 'bill', 'person_id', Person.find(:all).collect {|p| [p.fullname, p.id]} %>


    #8

    Eero S. wrote:

    Spectre013 wrote:

    I was modestly intrigued by the question…
    here is the more-or-less final version I
    came up with since I wanted a one-liner:

    require ‘date’
    require ‘time’

    def weekdays()
    ((Date.today - 6)…today).inject({}) {|h, d| h[d.strftime("%A")] =
    Time.parse(d).to_i; h}
    end # weekdays()

    weekdays[‘Monday’] # etc.

    Of course, it might be desirable to use some
    other kind of URL rather than Time#to_i, for
    example just ‘2005/12/18’, for example. That
    should be simple enough to substitute for the
    Time.parse call.

    E