0

在我的 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"}]]

并查看:

=@oem_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]

但我怎样才能以更美丽的方式做到这一点?另外如何用空键跳过散列?我怎样才能通过标准的一个循环来做到这一点,例如:

=@oem_art.each do |oem|
          = oem.car
          %td
            =oem.price  

怎么弄平?但是这里没用...

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
4

2 回答 2

0

美是个人品味的问题,并且flatten对于避免嵌套循环有些用处。

- @oem_art.flatten.reject { |h| h[:car].empty? }.each do |oem|
  = oem[:car]
  # And so on ...
于 2012-12-11T20:55:28.333 回答
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 的重复

于 2012-12-11T21:49:36.523 回答