gpt4 book ai didi

ruby-on-rails - 如何使用与 gem 同名的模型?

转载 作者:行者123 更新时间:2023-12-04 17:02:16 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





module and class with the same name in Rails project

(2 个回答)


4年前关闭。




我看到了这个问题,但没有帮助:What is the fully qualified name of a model in Ruby on Rails?

我有这个代码

# create Stripe customer
customer = Stripe::Customer.create(:email => @user.email, :source => params['subscription']['token'])
stripe_subscription = Stripe::Subscription.create(:customer => customer.id, :plan => @subscription.plan)
stripe = ::Stripe.new subscription: @subscription, customer_id: customer.id, plan_id: @subscription.plan,
stripe_subscription_id: stripe_subscription.id, last_paid: Time.now, status: stripe_subscription.status

Stripe 是一颗 gem ,但它也是我模特的名字。我试过 ::Stripe.new以上,我也试过 stripe = @subscription.build_stripe ... .他们都给出了错误

undefined method `new' for Stripe:Module

最佳答案

你不能。顶级命名空间中不能存在同名的模块和类。如果你真的让 Rails 加载你的 Stripe模块,您的应用程序将因类型错误而崩溃,并提示您试图更改 Stripe 的类型从模块到类。

关于ruby-on-rails - 如何使用与 gem 同名的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com