# Associative arrays

**URL:** <https://www.ruby-forum.com/t/associative-arrays/50471>\
**Category:** Rails\
**Created:** [December 16, 2005, 5:54pm UTC](https://www.ruby-forum.com/t/associative-arrays/50471 "2005-12-16T17:54:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 16, 2005, 5:54pm UTC](https://www.ruby-forum.com/t/associative-arrays/50471/1 "2005-12-16T17:54:41Z")

</div>

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](https://www.ruby-forum.com/index/<%=%20days['Monday']%20%>)
- [Tuesday](https://www.ruby-forum.com/index/<%=%20days['Tuesday']%20%>)
- [Wednesday](https://www.ruby-forum.com/index/<%=%20days['Wednesday']%20%>)
- [Thursday](https://www.ruby-forum.com/index/<%=%20days['Thursday']%20%>)
- [Friday](https://www.ruby-forum.com/index/<%=%20days['Friday']%20%>)
- [Saturday](https://www.ruby-forum.com/index/<%=%20days['Saturday']%20%>)
- [Sunday](https://www.ruby-forum.com/index/<%=%20days['Sunday']%20%>)

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

Thanks for your help

---

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 16, 2005, 10:33pm UTC](https://www.ruby-forum.com/t/associative-arrays/50471/2 "2005-12-16T22:33:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 16, 2005, 11:24pm UTC](https://www.ruby-forum.com/t/associative-arrays/50471/3 "2005-12-16T23:24:45Z")

</div>

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() %\>

---

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 17, 2005, 12:46am UTC](https://www.ruby-forum.com/t/associative-arrays/50471/4 "2005-12-17T00:46:53Z")

</div>

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 🙂
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 17, 2005, 2:27am UTC](https://www.ruby-forum.com/t/associative-arrays/50471/5 "2005-12-17T02:27:41Z")

</div>

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 60_60_24\*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

---

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 18, 2005, 10:09am UTC](https://www.ruby-forum.com/t/associative-arrays/50471/6 "2005-12-18T10:09:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 18, 2005, 9:38pm UTC](https://www.ruby-forum.com/t/associative-arrays/50471/7 "2005-12-18T21:38:52Z")

</div>

On 12/18/05, Ivan S. [removed\_email\_address@domain.invalid](mailto: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]} %\>

---

<div class="post-metadata">

**Author:** ![Spectre013](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@Spectre013](https://www.ruby-forum.com/u/Spectre013)\
**Post date:** [December 18, 2005, 6:31am UTC](https://www.ruby-forum.com/t/associative-arrays/50471/8 "2005-12-18T06:31:34Z")

</div>

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
