2012-12-11 74 views
0

在我的Rails應用程序現在我有這樣的數據:Rails的看法哈希陣列

[[{:car=>"", :article=>"", :group=>"", :price=>""}, 
    {:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}, 
    {:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}, 
    {:car=>"Volkswagen", :article=>"7H0127401D", :group=>"FILTER", :price=>"0,42"}], 
[{:car=>"", :article=>"", :group=>"", :price=>""}, 
    {:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}], 
[{:car=>"", :article=>"", :group=>"", :price=>""}, 
    {:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}]] 

,並查看:

[email protected]_art.each do |oem| 
     -oem.each do |oo| 
     %tr{:class => cycle("zebra-stripe zebra-grey zebra1", "zebra-stripe zebra-grey zebra2")} 
      %td 
      = oo[:car] 
      %td 
      %h4 
       = oo[:article]    
      %td 
      = oo[:group] 
      %td 
      = oo[:price] 

但我怎麼能做到這一點更beatiful方式?另外如何跳過空鍵的散列? 如何通過非標準一個循環做到這一點,例如:

[email protected]_art.each do |oem| 
      = oem.car 
      %td 
      =oem.price 

如何扁平化?但它是unusefull這裏...

UPD

def original_art 
    @article = Article.find_by_ART_ID(params[:id]) 
    @constr_num = ArtLookup.get_construction_number(@article.ART_ID) 
    oem_art = [] 
    @constr_num.each do |o| 
     as_oem = get_from_as_oem(o.ARL_SEARCH_NUMBER) 
     if as_oem.present? 
     oem_art << as_oem 
     end 
    end  
    @oem_art = oem_art.to_a.uniq 
    @article = Article.first 
    @oem_art 
    end 
+0

'flatten'應該可以幫到這裏嗎?你爲什麼說它沒用? – mikej

+0

看看你發佈的更新後的代碼,你不應該需要'to_a',因爲'oem_art'已經是一個數組了,'uniq'可能沒有做你想做的事情,因爲它不會去除重複的哈希值重複散列數組。 – mikej

+0

@mikej沒有100%的理解你寫在代碼中.. – byCoder

回答

0

美是個人品位的事,flatten是一些使用,以避免嵌套的循環。

- @oem_art.flatten.reject { |h| h[:car].empty? }.each do |oem| 
    = oem[:car] 
    # And so on ... 
+0

這幾乎是我會建議的,但它也值得看看其餘的代碼,看看你是否可以避免獲得空的條目擺在首位。 – mikej

+0

我添加了一段代碼...在那裏調用另一種方法...對不起,只會在下午 – byCoder

0

這是我在這個問題的評論中提出的觀點。如果你有oem_art

[[{:car=>"", :article=>"", :group=>"", :price=>""}, 
    {:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}, 
    {:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}, 
    {:car=>"Volkswagen", :article=>"7H0127401D", :group=>"FILTER", :price=>"0,42"}], 
[{:car=>"", :article=>"", :group=>"", :price=>""}, 
    {:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}], 
[{:car=>"", :article=>"", :group=>"", :price=>""}, 
    {:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}]] 

即要素是:

1. [{:car=>"", :article=>"", :group=>"", :price=>""}, 
     {:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}, 
     {:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}, 
     {:car=>"Volkswagen", :article=>"7H0127401D", :group=>"FILTER", :price=>"0,42"}] 

2. [{:car=>"", :article=>"", :group=>"", :price=>""}, 
     {:car=>"Volkswagen", :article=>"7H0127401A", :group=>"FILTER", :price=>"0,564"}] 

3. [{:car=>"", :article=>"", :group=>"", :price=>""}, 
     {:car=>"Volkswagen", :article=>"7H0127401B", :group=>"FILTER", :price=>"0,546"}] 

如果調用oem_art.uniq這將消除重複的元素,但3個元素都是不同的,所以什麼都不會被刪除。它不會從元素中刪除重複的7H0127401A和7H0127401B。

+0

所以......你的解決? – byCoder

+0

這並不意味着要成爲您問題的完整答案。我只是寫了一個較長的版本,你說你不明白100%。乾杯。 – mikej