2011-05-05 86 views
1

在我的Rails應用程序中,我使用Braintree創建訂閱。沒有意識到,我還創建了一個Subscription模型和控制器來管理我想要在本地存儲的訂閱信息。在我的模型中,訂閱可以屬於用戶。然而,一些正常的東西,你可以做的是不工作,如 current_user.subscriptions.build()Rails Braintree寶石訂閱類衝突

但是,當有人在幫我,他們能夠使用

current_user.create_subscription 

如果由於某種原因這是create_subscription方法的定義嗎?是否以某種方式覆蓋Rails約定?

我注意到Braintree gem中有一個subscription.rb文件。與Braintree和我的訂閱模型定義的類有衝突嗎?我知道我可以重新命名我的訂閱模型,但我很好奇衝突是什麼。

回答

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). 

布倫特裏確實有一個訂閱類,但這命名空間布倫特裏:認購所以這不是問題。

+0

啊,我明白了。不知道這跟has_one有什麼不同。謝謝! – NicSlim 2011-05-05 21:16:02