我正在使用RubyonRails和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