2011-05-05 13 views
1

私のRailsアプリケーションでは、Braintree gemを使用してサブスクリプションを作成しています。わからなくても、ローカルに保存したい購読情報を管理するSubscriptionモデルとコントローラを作成しました。私のモデルでは、サブスクリプションはユーザーに帰属することができます。しかし、あなたが行うことができ、通常のもののいくつかは、そのような current_user.subscriptions.build()Rails Braintree gemサブスクリプションクラスの競合

として働いて誰かが、彼らは

current_user.create_subscription 

使用することができました私を助けたいくつかの理由ではなかったですこのcreate_subscriptionメソッドは定義されていますか?それは何とかRailsのコンベンションをオーバーライドしていますか?

Braintreeの宝石にsubscription.rbファイルがあることに気付きました。 BraintreeとSubscriptionモデルで定義されているクラスと競合していますか?おそらく私のサブスクリプションモデルの名前を変更するだけかも知りませんが、私はその競合が何であるか興味があります。

答えて

1

あなたの問題は、サブスクリプションの関係がhas_manyではなく、has_oneまたはbelongs_toであることです。添付されたサブスクリプションは単数であるため、ユーザーはこの場合サブスクリプションメソッドを持たないでしょう。 ARでこれらの種類の関係を操作する方法については、APIドキュメントを参照してください。

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