PSC*_*ell 2 ruby ruby-on-rails
我有一个具有付费布尔属性的发票模型.帐户和发票之间存在关联,我只想在视图中显示未付款的发票.
我用它来显示所有发票:
<table class="table">
<thead>
<tr>
<th>Invoice Number</th>
<th>Invoice Date</th>
<th>Invoice Total</th>
</tr>
</thead>
<tbody>
<% @account.invoices.each do |invoice| %>
<tr>
<td><%= invoice.id%> </td>
<td><%= invoice.created_at.time.to_formatted_s(:long)%> Zulu </td>
<td><%= invoice.total %></td>
</tr>
<% end %>
</tbody>
Run Code Online (Sandbox Code Playgroud)
但我不确定如何将结果限制在只paid显示nil的发票的地方.我试过:<% @account.invoices.each do |invoice| unless @account.invoices.paid == 'nil' %>但是遇到了错误.显然我的语法错了.有什么建议?
你可以named scope在这种情况下使用.
unpaid在invoices模型中创建了一个范围:
scope :unpaid, -> { where( paid: [nil, false] ) }
Run Code Online (Sandbox Code Playgroud)
并在视图中使用它,如下所示:
<% @account.invoices.unpaid.each do |invoice| %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
564 次 |
| 最近记录: |