2009-06-11 5 views
0

하나의 부모로부터 동일한 자식에 대해 여러 개의 다형성 관계 (has_many_polymorphs plugin)를 정의하려고합니다.하나의 모델에 여러 개의 has_many_polymorphs가 있습니다.

참고 많은 시청자가
참고 많은 편집자가 어느 사용자 또는 그룹이 될 수
시청자
권한이 note_id, viewer_id, viewer_type, editor_id, editor_type 필드 협회의 모델 중 하나를 사용자 또는 그룹이 될 수
편집자 한 내가 참고 모델에 정의 된 하나 개의 has_many_polymorphs 관계를 가지고 예상대로

모든 밖으로 작동

이제 문제는 내가 두 번째 관계

class Note < ActiveRecord::Base 
    has_many_polymorphs :viewers, :through => :permissions, :from => [:users, :groups] 
    has_many_polymorphs :editors, :through => :permissions, :from => [:users, :groups] 
end 

class Permission < ActiveRecord::Base 
    belongs_to :note 
    belongs_to :viewer, :polymorphic => true 
    belongs_to :editor, :polymorphic => true 
end 

Note.first.viewers << User.first # => [#<User id: ....>] 

# >>>>>>>> 
Note.first.editors << User.first 

NoMethodError: You have a nil object when you didn't expect it! 
The error occurred while evaluating nil.constantize 
... vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/base.rb:18:in `instantiate' 

내가 has_many_polymorphs의 정의를 정제 시도했지만 작동하지 않았다을 추가 할 때 나타나기 시작. ViewPermission < PermissionEditPermission < Permission의 STI 모델조차도 아닙니다.

모든 생각/문제 해결 방법/문제 포인터는 높이 평가됩니다.

레일 2.3.0

+0

난 당신이 –

답변

0

그나마 당신은 당신의 참고

has_many :permissions 

를 추가해야합니다. FYI. 나는 has_many_polymorphs을 한 번 사용한 다음 떨어 뜨 렸지만 예상대로 작동하지 않았습니다.

사용 권한에 사용중인 스키마를 게시 할 수 있습니까? 내 생각 엔이 문제의 근원은 거기에 놓여있다. 두 개의 다른 belongs_to이 정의에 있기 때문에 스키마에 여러 유형의 ID 쌍이 있어야한다.

편집 : 당신은뿐만 아니라 GitHub의에 질문을 게시 한 볼

. 양면 다형성을 사용해 보았는지 확실하지 않습니다. 당신은 아마도 ... 내가 말했듯이, 내가이 플러그인을 사용했을 때 약간의 불안정성을 초래했기 때문에 나는이 플러그인에 감명을받지 않았다. @Tamer

== Double-sided polymorphism 

Double-sided relationships are defined on the join model: 

     class Devouring < ActiveRecord::Base 
     belongs_to :guest, :polymorphic => true 
     belongs_to :eaten, :polymorphic => true 

     acts_as_double_polymorphic_join(
      :guests =>[:dogs, :cats], 
      :eatens => [:cats, :birds] 
     )  
     end 


Now, dogs and cats can eat birds and cats. Birds can't eat anything (they aren't <tt>guests</tt>) and dogs can't be 
eaten by anything (since they aren't <tt>eatens</tt>). The keys stand for what the models are, not what they do. 
+0

은'has_many는 ... 두 번째 단락에서 일부 문장 부호를 추가 할 필요가 있다고 생각. 두 번째 단락에 설명 된대로 스키마는 다음과 같습니다 정수 : 정수 note_id : viewer_type 정수 : 문자열 \t editor_id : 문자열 \t이 viewer_id editor_type – tamersalama

+0

감사 라이언 - 예 - 내가 문제로 GitHub의에 게시 (HTTP ://github.com/fauna/has_many_polymorphs/issues/#issue/3) 양면 관계가 작동하는지 잘 모르겠습니다. 내 _ 결정은 다형성 부모 관계를위한 것입니다. ie : 단지'Note'를 부모로 가지는 대신'Post'를 부모로 말할 수 있습니다; 그것은 양면 다형성이 될 것이다. 메모에는 사용자 또는 그룹 뷰어가있을 수 있습니다. 포스트는 사용자 또는 그룹 뷰어를 가질 수 있습니다. 관계 테이블'Permission'은'authorizable_id'와'authorizable_type'을 가지고'Note' 또는'Post' 참조를 저장해야합니다. – tamersalama

0

저도 같은 오류가 발생했다. 문제는 has_many_polymorphs이 대량 연결을 사용하여 조인 테이블에 레코드를 생성하고 실패했기 때문입니다. 나는 Permission 클래스에 attr_accessible :note_id, :editor_id:editor_type을 추가하고 이후에 작업했습니다. (참고 : 내 모델 이름을 대체했습니다.)

너무 많이 보지 않았지만이 동작을 변경하는 방법이 있다면 궁금 할 것입니다. 나는이 프레임 워크에 상당히 익숙하며 대량 주문 (예 : 주문 결제 협회)과 같은 민감한 사항은 발에서 자신을 쏘라고 요구하는 것처럼 보입니다. 이게 당신의 문제를 해결하고 다른 것을 알아 내면 알려주세요.permissions` 필요하지 않습니다 :

최저

,
스티브

관련 문제