Rails:列に値があるかどうかを確認するにはどうすればよいですか?
-
08-07-2019 - |
質問
どうすればこれを達成できますか?
<% for agent in @broker.agents %>
...
<% if agent.cell %><span class="cell-number">Cell: <%= agent.cell %></span><% end %>
...
<% end %>
エージェントにセル番号があるかどうかをテストし、ある場合は条件内の内容を表示します。私が現在持っているものはうまくいかないようです。 &quot; Cell:&quot;と表示されるだけです。
思考?
解決
これはあなたが求めたものです:
<% for agent in @broker.agents %>
<% unless agent.cell.blank? %>
<span class="cell-number">Cell: <%= agent.cell %></span>
<% end %>
<% end %>
セル?メソッドは、セルがnilでも空の文字列でも機能します。 Railsは、すべてのActiveRecord属性に同様の機能を追加します。これは少し良く見えるでしょう:
<% for agent in @broker.agents %>
<span class="cell-number">
Cell: <%= agent.cell? ? "none given" : agent.cell %>
</span>
<% end %>
疑問符とコロンは、簡単な&quot; if?それから:else&quot;ステートメント。 1つはメソッド名セルの一部であるため、上記のコードには2つの疑問符がありますか?もう一方はif / then / else構文の一部です。
他のヒント
この質問に対して非常に詳細な回答をしています&quot; 列に値があるかどうかを確認するにはどうすればよいですか&quot;。
まず、属性には4種類の値を含めることができることに注意することが重要です。
- nil 値、つまり「nil」データベースに保存
- 空の値、つまり&quot;&quot; スペースなし の空の文字列
- 空文字列スペース付き&quot; &quot;。
- データベースに存在する値、つまり空でない文字列。
この場合に使用できるすべての現在のメソッド(Ruby 2.2.2)の詳細な動作を次に示します。
最初の方法: .empty?
-
nil の場合は、値=&gt;例外をスローします
2.2.2 :037 > object.attribute => nil 2.2.2 :025 > object.attribute.empty? NoMethodError: undefined method `empty?' for nil:NilClass
-
empty の値、つまり&quot;&quot;の場合スペースなし
の空の文字列2.2.2 :037 > object.attribute => "" 2.2.2 :025 > object.attribute.empty? true
-
空文字列スペースあり&quot; &quot;。
2.2.2 :041 > object.attribute => " " 2.2.2 :042 > object.attribute.empty? => false
-
データベースに存在する値、つまり空でない文字列。
2.2.2 :045 > object.attribute => "some value" 2.2.2 :046 > object.attribute.empty? => false
2番目の方法: .nil?
-
nil 値、つまり「nil」データベースに保存
2.2.2 :049 > object.attribute => nil 2.2.2 :050 > object.attribute.nil? => true
-
empty の値、つまり&quot;&quot; スペースなし
の空の文字列2.2.2 :053 > object.attribute => "" 2.2.2 :054 > object.attribute.nil? => false
-
空文字列スペースあり&quot; &quot;。
2.2.2 :057 > object.attribute => " " 2.2.2 :058 > object.attribute.nil? => false
-
データベースに存在する値、つまり空でない文字列。
2.2.2 :061 > object.attribute => "some value" 2.2.2 :062 > object.attribute.nil? => false
3番目の方法: .blank?
-
nil 値、つまり「nil」データベースに保存
2.2.2 :065 > object.attribute => nil 2.2.2 :066 > object.attribute.blank? => true
-
empty の値、つまり&quot;&quot; スペースなし
の空の文字列2.2.2 :069 > object.attribute => "" 2.2.2 :070 > object.attribute.blank? => true
-
空文字列スペースあり&quot; &quot;。
2.2.2 :073 > object.attribute => " " 2.2.2 :074 > object.attribute.blank? => true
-
データベースに存在する値、つまり空でない文字列。
2.2.2 :075 > object.attribute => "some value" 2.2.2 :076 > object.attribute.blank? => false
4番目の方法: .present?
-
nil 値、つまり「nil」データベースに保存
2.2.2 :088 > object.attribute => nil 2.2.2 :089 > object.attribute.present? => false
-
empty の値、つまり&quot;&quot; スペースなし
の空の文字列2.2.2 :092 > object.attribute => "" 2.2.2 :093 > object.attribute.present? => false
-
空文字列スペースあり&quot; &quot;。
2.2.2 :096 > object.attribute => " " 2.2.2 :097 > object.attribute.present? => false
-
データベースに存在する値、つまり空でない文字列。
2.2.2 :100 > object.attribute => "some value" 2.2.2 :101 > object.attribute.present? => true
直面している状況に応じて、4つのいずれかを使用できます。
ありがとう
if !agent.cell.blank?
動作します。
agent.cell? agent.cell.blankと同じように動作するようですか? RoRで。
<% @broker.agents.each do |agent| %>
...
<% unless agent.cell.empty? %>
<span class="cell-number">Cell: <%= agent.cell %></span>
<% end %>
...
<% end %>
#each、 unless
、および cell.empty?
を使用すると、一見読みやすくなり、理解しやすくなります。