代码之家  ›  专栏  ›  技术社区  ›  Blankman

表中有一列包含图像的url,但有些行不包含,如何检查?

  •  0
  • Blankman  · 技术社区  · 14 年前

    我正在创建一个包含图像URL的XML文件,在编写XML时必须解析这个URL。

    类似于:

    images = Image.limit(100)
    
    images.each { |i|
    
      blah = ''
    
      if i.url ???
        # process stuff here
      else
          blah = ''
      end
    
    
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   the Tin Man    14 年前

    如果是SQL数据库,我将使用跳过缺少URL的行的查询:

     select * from database_table where url is not null
    

    或者类似的东西。数据库查询因数据库而异。

    否则,在Ruby中我会做如下事情:

    if (i.url.any?)
        ...
    else
        ...
    end
    

    if (!i.url.empty?)...
    
    推荐文章