default_scope
ページに表示される列を柔軟に使用したいと考えていcustomer index
ます。たとえば、ユーザーに customer が表示されないようにする場合は、ページからphone#
削除phone
しdefault_scope
てphone
空白のままにしindex
ます。このアイデアはオンラインから得て、テストを行っています。missing attribute:phone
ただし、を選択するとエラーが発生しますphone
。モデルdefault_scope
内の定義は次のとおりです。customer
def self.default_scope
Customer.scoped.select("active,name,sales_id,customer_status_category_id, short_name, zone_id, id, since_date")
end
インデックスページ:
<table>
<tr>
<th>#</th>
<th>ShortName</th>
<th>SinceDate</th>
<th>Phone</th>
<th>Active?</th>
<th>CustomerStatusCategory</th>
<th>Sales</th>
<th>LastContactDate</th>
</tr>
<% @customers.each do |cust| %>
<tr>
<td><%= cust.id %></td>
<td><%= cust.short_name %></td>
<td><%= cust.since_date.strftime("%Y/%m/%d") %></td>
<td><%= cust.phone if cust.phone.present? %></td>
<td><%= cust.active %></td>
<td><%= cust.customer_status_category.name %></td>
<td><%= cust.sales.name %></td>
<td><%= return_last_contact_date(cust.id).strftime("%Y/%m/%d") %></td>
</tr>
<% end %>
</table>
エラーは次のとおりです。
ActiveModel::MissingAttributeError in Customerx/customers#index
Showing C:/D/code/rails_proj/engines/customerx/app/views/customerx/customers/_index_partial.html.erb where line #32 raised:
missing attribute: phone
index
(または任意のビュー ページ)で意図的に選択しないことで空の列を表示することは可能ですか? 可能であれば、それdefault_scope
を達成するための適切なツールはありますか? ありがとう。