我正在使用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
句话看起来很傻但无法解释为什么(我白天不是开发人员,我只是涉猎)
你的直觉是正确的,这很愚蠢!
def has_card? Stripe::Customer.retrieve(stripe_customer_id).cards.count > 0 end