在我的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
'flatten'應該可以幫到這裏嗎?你爲什麼說它沒用? – mikej
看看你發佈的更新後的代碼,你不應該需要'to_a',因爲'oem_art'已經是一個數組了,'uniq'可能沒有做你想做的事情,因爲它不會去除重複的哈希值重複散列數組。 – mikej
@mikej沒有100%的理解你寫在代碼中.. – byCoder