# Using ActiveRecord to populate an array

**URL:** <https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434>\
**Category:** Rails\
**Created:** [November 9, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434 "2006-11-09T21:53:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 9, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/1 "2006-11-09T21:53:42Z")

</div>

CURRENCIES = []  
Currency.find(:all) {|x| CURRENCIES \<\< [x.name + " (" + x.symbol + “)”,  
x.id]}

Yet my array stays empty. Seems like I’m missing a step between the  
find all and the code block. Any suggestions for this rookie? Thanks!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 9, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/2 "2006-11-09T21:57:56Z")

</div>

Fixed it:

CURRENCIES = []  
@temp = Currency.find(:all)  
@temp.each {|x| CURRENCIES \<\< [x.name + " (" + x.symbol + “)”, x.id]}

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 9, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/3 "2006-11-09T22:32:32Z")

</div>

On Nov 9, 2006, at 3:57 PM, Taylor S. wrote:

> Fixed it:
> 
> CURRENCIES = []  
> @temp = Currency.find(:all)  
> @temp.each {|x| CURRENCIES \<\< [x.name + " (" + x.symbol + “)”, x.id]}

You could probably just:

CURRENCIES = Currency.find(:all).map { |x| [“#{x.name} (#  
{x.symbol})”, x.id] }

You almost certainly don’t want to use an instance variable (@temp)  
where a local variable (temp) would do, but all you were really  
missing in your first post was the .each on the result of your find  
(:all)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 9, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/4 "2006-11-09T23:14:43Z")

</div>

Thanks. This works great and is much more succinct:

CURRENCIES = Currency.find(:all).map {|x| [x.symbol + " - " + x.name,  
x.id]}

Care to get fancier? A symbol is between 1 - 3 characters long  
resulting in ugly uneven column of names next to the symbols. Since  
this array will be used to populate a select, I am trying to even it out  
with spaces:

Currency.find(:all).map {|x| [x.symbol + (4 - x.symbol.length).times do  
{"+ "} + x.name, x.id]}

However I get a syntax error after the second set of brackets. How can  
I put the spacing code block inside the array creation code block?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 9, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/5 "2006-11-09T23:24:47Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Taylor S. wrote:

> CURRENCIES = []  
> Currency.find(:all) {|x| CURRENCIES \<\< [x.name + " (" + x.symbol + “)”,  
> x.id]}
> 
> Yet my array stays empty. Seems like I’m missing a step between the  
> find all and the code block. Any suggestions for this rookie? Thanks!

You’re passing a block into the “find” method, you’re not iterating over  
the results.

It would work if you inserted an “each” after the “(:all)”.

ie:

Currency.find(:all).each {|x| CURRENCIES \<\< [x.name + " (" + x.symbol +  
“)”, x.id]}  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.3 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFU6kxMyx0fW1d8G0RAkzGAJ40yo4kl71JiAChG2SNDxHtv51N6QCfYpK5  
iwbdR6gZiH1Wc15tDtbXiqQ=  
=T/pe  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 9, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/6 "2006-11-09T23:18:40Z")

</div>

Actually this method won’t work because Ruby doesn’t interpret UTF-8  
character length the right way. Of course, it would still be nice to  
know how to nest blocks!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 10, 2006, 12:06am UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/7 "2006-11-10T00:06:54Z")

</div>

On 11/9/06, Taylor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually this method won’t work because Ruby doesn’t interpret UTF-8  
> character length the right way. Of course, it would still be nice to  
> know how to nest blocks!

Rails 1.2 includes multibyte chars support:  
x.symbol.chars.length

jeremy

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 9, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/using-activerecord-to-populate-an-array/75434/8 "2006-11-09T23:58:53Z")

</div>

On Nov 9, 2006, at 5:14 PM, Taylor S. wrote:

> with spaces:
> 
> Currency.find(:all).map {|x| [x.symbol + (4 -  
> x.symbol.length).times do  
> {"+ "} + x.name, x.id]}
> 
> However I get a syntax error after the second set of brackets. How  
> can  
> I put the spacing code block inside the array creation code block?

For the UTF-8, are you doing:

$KCODE = ‘UTF8’  
require ‘jcode’

Then this should work:  
CURRENCIES = Currency.find(:all).map do |x|  
[“%s%\*s %s”.%([x.symbol, 4-x.symbol.jlength, ‘-’, x.name]), x.id ]  
end

look at String#% or Kernel.sprintf for the formatting details

This may not work if your UTF-8 characters are of varying display  
widths in your browser font anyway, but it might be close enough.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
