Rails Braintree gem 订阅类冲突
在我的 Rails 应用程序中,我使用 Braintree gem 来创建订阅。不知不觉中,我还创建了一个订阅模型和控制器来管理我想在本地存储的订阅信息。在我的模型中,订阅可以属于用户。然而,您可以做的一些正常的事情却不起作用,例如 current_user.subscriptions.build()
但由于某种原因,当有人帮助我时,他们能够使用
current_user.create_subscription
这个 create_subscription 方法在哪里定义?它是否以某种方式超越了 Rails 约定?
我注意到 Braintree gem 中有一个 subscription.rb 文件。与 Braintree 定义的类和我的订阅模型是否存在冲突?我知道我可能可以重命名我的订阅模型,但我很好奇冲突是什么。
In my Rails app, I'm using the Braintree gem to create subscriptions. Without realizing, I had also created a Subscription model and controller to manage the subscription info I wanted to store locally. In my model, a subscription can belong_to a user. However, some of the normal stuff you can do wasn't working such as
current_user.subscriptions.build()
But for some reason when someone was helping me they were able to use
current_user.create_subscription
Where is this create_subscription method defined? Is it somehow overriding the Rails convention?
I noticed that there is a subscription.rb file in the Braintree gem. Is there some conflict with the class defined by Braintree and my Subscription model? I know that I can probably just rename my Subscription model, but I'm curious as to what the conflict is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是订阅关系是has_one或belongs_to,而不是has_many。在这种情况下,用户将没有订阅方法,因为附加的订阅将是单一的。查看 API 文档,了解如何在 AR 中操纵此类关系。
从 has_one 上的手册来看:
Braintree 确实有一个 Subscription 类,但它的命名空间为 Braintree:Subscription,所以这不是问题。
Your issue is that the subscription relation is has_one or belongs_to, rather than has_many. User would not have a subscriptions method in this case as the attached subscription would be singular. Review the API docs for how to manipulate these sorts of relations in AR.
From the manual on has_one:
Braintree does have a Subscription class, but this is namespaced to Braintree:Subscription so it is not the issue.