【问题标题】:Rails: How do I check if a column has a value?Rails:如何检查列是否有值?
【发布时间】:2010-09-23 02:23:16
【问题描述】:

我怎样才能做到这一点?

<% for agent in @broker.agents %>
  ...
  <% if agent.cell %><span class="cell-number">Cell: <%= agent.cell %></span><% end %>
  ...
<% end %>

我想测试一下代理是否有单元格编号,如果有,则显示条件中的内容。我目前所拥有的似乎不起作用;它只显示“单元格:”。

想法?

【问题讨论】:

    标签: ruby-on-rails ruby database ruby-on-rails-3 ruby-on-rails-4


    【解决方案1】:
    if !agent.cell.blank?
    

    有效。

    【讨论】:

    • 您可能会考虑“除非 agent.cell.blank?”,有些断言是“更像 Ruby-ish”
    • 我使用除非,除非 agent.cell.blank 因为双重否定非常棒。
    • 你也可以使用if agent.cell.present?#present?返回 #blank 的反义词?
    【解决方案2】:

    这是你要求的:

    <% 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 %>
    

    问号和冒号组成一个快速的“if ? then : else”语句。上面的代码中有两个问号,因为一个是方法名称单元格的一部分?另一个是 if/then/else 结构的一部分。

    【讨论】:

    • 你可以通过&lt;%= agent.cell || "none given" %&gt;来简化这个
    【解决方案3】:

    agent.cell?似乎与agent.cell.blank一样工作?在 RoR 中。

    【讨论】:

    • 它似乎不在 rails 3.2.6(或 ruby​​ 1.9)
    【解决方案4】:
    <% @broker.agents.each do |agent| %>
      ...
      <% unless agent.cell.empty? %>
        <span class="cell-number">Cell: <%= agent.cell %></span>
      <% end %>
      ...
    <% end %>
    

    我发现使用#each、unlesscell.empty? 乍一看更易于阅读和理解。

    【讨论】:

      【解决方案5】:

      我正在对这个问题“如何检查列是否有值?”给出一个非常详细的答案。

      首先需要注意的是,一个属性可以有四种值。

      1. nil 值,即存储在数据库中的“nil”
      2. empty 值,即 "" 一个空字符串,没有空格
      3. 字符串带空格“”。
      4. 数据库中存在的值,即非空字符串

      这里是可以在这种情况下使用的所有当前方法(Ruby 2.2.2)的详细行为。

      第一种方法:.empty?

      1. 对于 nil 值 => 抛出异常

        2.2.2 :037 > object.attribute
        => nil
        2.2.2 :025 > object.attribute.empty?
        NoMethodError: undefined method `empty?' for nil:NilClass
        
      2. 对于 empty 值,即 "" 一个 没有空格

        的空字符串
        2.2.2 :037 > object.attribute
        => ""
        2.2.2 :025 > object.attribute.empty?
        true
        
      3. 字符串带空格“”。

        2.2.2 :041 > object.attribute
        => " " 
        2.2.2 :042 > object.attribute.empty?
        => false
        
      4. 数据库中存在的值,即非空字符串

        2.2.2 :045 > object.attribute
         => "some value" 
        2.2.2 :046 > object.attribute.empty?
         => false 
        

      第二种方法:.nil?

      1. nil 值,即存储在数据库中的“nil”

        2.2.2 :049 > object.attribute
         => nil 
        2.2.2 :050 > object.attribute.nil?
         => true
        
      2. empty 值,即 "" 一个空字符串,没有空格

        2.2.2 :053 > object.attribute
         => "" 
        2.2.2 :054 > object.attribute.nil?
         => false 
        
      3. 字符串带空格“”。

        2.2.2 :057 > object.attribute
         => " " 
        2.2.2 :058 > object.attribute.nil?
         => false 
        
      4. 数据库中存在的值,即非空字符串

        2.2.2 :061 > object.attribute
         => "some value" 
        2.2.2 :062 > object.attribute.nil?
         => false
        

      第三种方法: .blank?

      1. nil 值,即存储在数据库中的“nil”

        2.2.2 :065 > object.attribute
         => nil 
        2.2.2 :066 > object.attribute.blank?
         => true
        
      2. empty 值,即 "" 一个空字符串,没有空格

        2.2.2 :069 > object.attribute
         => "" 
        2.2.2 :070 > object.attribute.blank?
         => true 
        
      3. 字符串带空格“”。

        2.2.2 :073 > object.attribute
         => " " 
        2.2.2 :074 > object.attribute.blank?
         => true 
        
      4. 数据库中存在的值,即非空字符串

        2.2.2 :075 > object.attribute
         => "some value" 
        2.2.2 :076 > object.attribute.blank?
         => false 
        

      第四法:.present?

      1. nil 值,即存储在数据库中的“nil”

        2.2.2 :088 > object.attribute
         => nil 
        2.2.2 :089 > object.attribute.present?
         => false
        
      2. empty 值,即 "" 一个空字符串,没有空格

        2.2.2 :092 > object.attribute
         => "" 
        2.2.2 :093 > object.attribute.present?
         => false
        
      3. 字符串带空格“”。

        2.2.2 :096 > object.attribute
         => " " 
        2.2.2 :097 > object.attribute.present?
         => false 
        
      4. 数据库中存在的值,即非空字符串

        2.2.2 :100 > object.attribute
         => "some value" 
        2.2.2 :101 > object.attribute.present?
         => true 
        

      你可以根据你所面临的情况使用这四种中的任何一种。

      谢谢

      【讨论】:

        【解决方案6】:

        您实际上可以通过执行检查该列是否包含任何内容 如果列类型是字符串,Model.column == "" 将返回 true

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多