Rails Braintree gem 订阅类冲突

发布于 2024-11-05 00:41:33 字数 425 浏览 4 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2024-11-12 00:41:33

您的问题是订阅关系是has_one或belongs_to,而不是has_many。在这种情况下,用户将没有订阅方法,因为附加的订阅将是单一的。查看 API 文档,了解如何在 AR 中操纵此类关系。

从 has_one 上的手册来看:

The following methods for retrieval and query of a single associated object will be added:

association(force_reload = false)

Returns the associated object. nil is returned if none is found.

association=(associate)

Assigns the associate object, extracts the primary key, sets it as the foreign key, and saves the associate object.

build_association(attributes = {})

Returns a new object of the associated type that has been instantiated with attributes and linked to this object through a foreign key, but has not yet been saved. Note: This ONLY works if an association already exists. It will NOT work if the association is nil.

create_association(attributes = {})

Returns a new object of the associated type that has been instantiated with attributes, linked to this object through a foreign key, and that has already been saved (if it passed the validation).

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:

The following methods for retrieval and query of a single associated object will be added:

association(force_reload = false)

Returns the associated object. nil is returned if none is found.

association=(associate)

Assigns the associate object, extracts the primary key, sets it as the foreign key, and saves the associate object.

build_association(attributes = {})

Returns a new object of the associated type that has been instantiated with attributes and linked to this object through a foreign key, but has not yet been saved. Note: This ONLY works if an association already exists. It will NOT work if the association is nil.

create_association(attributes = {})

Returns a new object of the associated type that has been instantiated with attributes, linked to this object through a foreign key, and that has already been saved (if it passed the validation).

Braintree does have a Subscription class, but this is namespaced to Braintree:Subscription so it is not the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文