Ruby on rails Ruby:在二维散列上迭代

Ruby on rails Ruby:在二维散列上迭代,ruby-on-rails,ruby,arrays,hash,Ruby On Rails,Ruby,Arrays,Hash,我正在尝试创建一个二维数组,就像这样 @transactions = {} @expenses.each do |expense| @transactions[expense.id.to_s + '-expense'] = {'identifier' => expense.id.to_s + '-expense', 'id' => expense.id, 'type' => 'expense', 'issued_on' => expense.issued_on, 'a

我正在尝试创建一个二维数组,就像这样

@transactions = {}
@expenses.each do |expense|
  @transactions[expense.id.to_s + '-expense'] = {'identifier' => expense.id.to_s + '-expense', 'id' => expense.id, 'type' => 'expense', 'issued_on' => expense.issued_on, 'amount' => expense.amount}
end

@invoices.each do |invoice|
  @transactions[invoice.id.to_s + '-invoice'] = {'identifier' => invoice.id.to_s + '-invoice', 'id' => invoice.id, 'type' => 'invoice', 'issued_on' => invoice.issued_on, 'amount' => invoice.total}
end
| 1-expense |
| identifier: 1-expense, id: 1, type: expense, issued_on: 2012-05-03, amount: 8.99 |
| 2-expense |
| identifier: 2-expense, id: 2, type: expense, issued_on: 2012-05-25, amount: 4.96 |
| 3-expense |
| identifier: 3-expense, id: 3, type: expense, issued_on: 2012-05-01, amount: 56.08 |
但是当我迭代数组来创建行时,它会首先给我一个键,然后是整个数组,就像这样

@transactions = {}
@expenses.each do |expense|
  @transactions[expense.id.to_s + '-expense'] = {'identifier' => expense.id.to_s + '-expense', 'id' => expense.id, 'type' => 'expense', 'issued_on' => expense.issued_on, 'amount' => expense.amount}
end

@invoices.each do |invoice|
  @transactions[invoice.id.to_s + '-invoice'] = {'identifier' => invoice.id.to_s + '-invoice', 'id' => invoice.id, 'type' => 'invoice', 'issued_on' => invoice.issued_on, 'amount' => invoice.total}
end
| 1-expense |
| identifier: 1-expense, id: 1, type: expense, issued_on: 2012-05-03, amount: 8.99 |
| 2-expense |
| identifier: 2-expense, id: 2, type: expense, issued_on: 2012-05-25, amount: 4.96 |
| 3-expense |
| identifier: 3-expense, id: 3, type: expense, issued_on: 2012-05-01, amount: 56.08 |
我迭代创建行当然,我应该做一些类似transaction.id或transaction.u的事情,但我会调试,这样你就可以明白我的意思了

<% @transactions.each do |transaction| %>
  <tr class="<%= cycle("odd", "even") %>">
    <td>
      <%= debug transaction %>
    </td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <% end %>
当我的桌子看起来像这样的时候

| identifier: 1-expense, id: 1, type: expense, issued_on: 2012-05-03, amount: 8.99 |
| identifier: 2-expense, id: 2, type: expense, issued_on: 2012-05-25, amount: 4.96 |
| identifier: 3-expense, id: 3, type: expense, issued_on: 2012-05-01, amount: 56.08 |

我不知道如何只创建一个包含事务数据行的简单表,而不创建包含哈希键的额外行

您使用{}语法创建了一个哈希,而不是数组

如果您只想在没有键的情况下迭代哈希值,可以执行以下操作:

<% @transactions.each_value do |transaction| %>
...
或者,可以使用数组来保存行:

@transactions = []
@expenses.each do |expense|
  @transactions << {'identifier' => expense.id.to_s + '-expense', 'id' => expense.id, 'type' => 'expense', 'issued_on' => expense.issued_on, 'amount' => expense.amount}
end

@invoices.each do |invoice|
  @transactions << {'identifier' => invoice.id.to_s + '-invoice', 'id' => invoice.id, 'type' => 'invoice', 'issued_on' => invoice.issued_on, 'amount' => invoice.total}
end
然后,您现有的erb模板应该工作得更好