如何重构这种方法?

 手机用户2502875153 发布于 2023-02-13 08:02

我正在使用Ruby on Rails和Stripe构建一个小型支付处理模块以获得乐趣,我想知道这个方法(用于检查给定用户是否已经使用Stripe存档的卡)可以重构:

class User < ActiveRecord::Base
...
def has_card?
    customer = Stripe::Customer.retrieve(self.stripe_customer_id)
    if customer.cards.count > 0
      true
    else
      false
    end
  end
end

我认为这if句话看起来很傻但无法解释为什么(我白天不是开发人员,我只是涉猎)

1 个回答
  • 你的直觉是正确的,这很愚蠢!

    def has_card?
      Stripe::Customer.retrieve(stripe_customer_id).cards.count > 0
    end
    

    2023-02-13 08:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有