Rails Braintree宝石订阅类冲突

时间:2011-05-05 16:07:28

标签: ruby-on-rails subscription braintree

在我的Rails应用程序中,我使用Braintree gem创建订阅。在没有意识到的情况下,我还创建了一个Subscription模型和控制器来管理我想在本地存储的订阅信息。在我的模型中,订阅可以属于用户。但是,你可以做的一些正常的东西是没有工作的     current_user.subscriptions.build()

但出于某种原因,当有人帮助我时,他们可以使用

current_user.create_subscription

这个create_subscription方法定义在哪里?它是否以某种方式凌驾于Rails惯例之上?

我注意到Braintree gem中有一个subscription.rb文件。是否与Braintree和我的订阅模型定义的类存在冲突?我知道我可能只是重命名我的订阅模型,但我很好奇冲突是什么。

1 个答案:

答案 0 :(得分:1)

您的问题是订阅关系是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,因此它不是问题。