私の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]
しかし、どうすればもっと美しい方法でそれを行うことができますか?また、空のキーでハッシュをスキップする方法は?たとえば、標準の1つのループを介してそれを行うにはどうすればよいですか?
=@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