Customer モデルと Device モデル、および Customer モデルと Device モデルの両方がありhas_many :devicesますbelongs_to :customer。customer.devices.empty?の場合は顧客のホームページにデバイスを追加するためのフォームを表示しようとしていtrueます。customer.devices.emptyfalse
私の問題は、customer.devices.empty?常に false を返すことです。いくつかのテストで、customer.devices.count常に正しい数のデバイスが表示されることがcustomer.devices.empty?わかりましたが、Rails コンソールを使用している間だけ、望ましい動作が得られます。
の値を簡単に確認できますが、意図したとおりにまたはチェックcustomer.devices.countを使用したいと思います。empty?any?
問題自体は記述されていますが、コードを見たい場合は...
<% if customer.devices.count == 0 %>
Count is 0 <!-- This is displayed on the page -->
<% end %>
<% if customer.devices.empty? %>
Customer has no devices! <!-- This is NOT displayed on the page -->
<% end %>
<% if customer.devices.any? %>
Customer has <%= pluralize(customer.devices.count, "device") %>.
<!-- The line above prints "Customer has 0 devices." -->
<% end %>
私のマナーをほとんど忘れていました-すべての回答に事前に感謝します。
-んん