Using ActiveRecord to populate an array

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!

Fixed it:

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

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
[email protected]

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?

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

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

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!

On 11/9/06, Taylor S. [email protected] 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

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
[email protected]