51224bdd17878b3b19e8987e9bb336a2

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

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 !

Be1e3ee645d23c95ba650c21bc885927

Fabien Jakimowicz

June 17, 2008, June 17, 2008 11:52, permalink

3 ratings. Login to rate!
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
880cbab435f00197613c9cc2065b4f5a

danielharan

June 17, 2008, June 17, 2008 13:47, permalink

No rating. Login to rate!

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?

Be1e3ee645d23c95ba650c21bc885927

Fabien Jakimowicz

June 17, 2008, June 17, 2008 15:32, permalink

2 ratings. Login to rate!

I thought the same about html select rails helper but it was supposed to render it flatten, so be it ;)

51224bdd17878b3b19e8987e9bb336a2

DG

June 18, 2008, June 18, 2008 04:42, permalink

No rating. Login to rate!

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

Your refactoring





Format Copy from initial code

or Cancel