1 2 3 4 5 6 7 8 9
carriers = Carrier.find(:all) @arr = [] carriers.each{|d| @arr << d.id @arr << d.name }
Refactorings
No refactoring yet !
Fabien Jakimowicz
June 17, 2008, June 17, 2008 11:52, permalink
1 2 3 4 5
# Rails < 2.0 Carrier.find(:all).collect {|d| [d.id, d.name]}.flatten # Rails >= 2.0.2 Carrier.all.collect {|d| [d.id, d.name]}.flatten
danielharan
June 17, 2008, June 17, 2008 13:47, permalink
While Fabien's solution does exactly what you asked, I'd venture that what you asked is not what you want or need. Leave off the call to flatten - can you make the place where you use this simpler now?
Fabien Jakimowicz
June 17, 2008, June 17, 2008 15:32, permalink
I thought the same about html select rails helper but it was supposed to render it flatten, so be it ;)
DG
June 18, 2008, June 18, 2008 04:42, permalink
Thanks Fabien for your refactoring.
I also need your help on this code snippet
http://www.refactormycode.com/codes/328-model-validations
Thanks In advance
In Following snippet I only want id & name of carriers as comma separated.
Following is the output I am expecting.
p @arr >> [1, "ATT Wireless", 2, "Alltel", 3, "Amritech", 4, "Boost", 5, "Cellular One"]