2011-05-05 5 views
1

레일스 앱에서 브레인 트리 보석을 사용하여 구독 정보를 생성합니다. 깨닫지 못한 채로 저는 로컬에 저장하려는 구독 정보를 관리하기 위해 구독 모델과 ​​컨트롤러도 만들었습니다. 내 모델에서 구독은 사용자에게 속할 수 있습니다. 그러나, 당신이 할 수있는 일반 물건의 일부는 다음과 같은 current_user.subscriptions.build()레일 브레인 트리 젬 가입 클래스 충돌

작동하지만되지 않은 누군가가

current_user.create_subscription 

어디를 사용할 수 있었다 나를 돕고 있었다 몇 가지 이유 이 create_subscription 메소드가 정의 되었습니까? Rails 컨벤션을 대체하는 것일까 요?

Braintree gem에 subscription.rb 파일이 있음을 발견했습니다. Braintree 및 Subscription 모델에 정의 된 클래스와 충돌이 있습니까? 아마도 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