2012-09-25 3 views
0

일부 사진을 표시하기 위해 작은 앱으로 작업하고 있습니다. 각 사진은 1에서 5까지의 눈금으로 "투표"또는 "수"할 수 있습니다.레일 및 몽고 이드, 자격 시스템 만들기 (두 모델에 자격 부여)

로깅 된 사용자에 대해 자격을 한 번만 수행 할 수 있습니다.

나는 각 이미지의 자격을 알아야하고, 어떤 이미지를 사용자가 투표를 설정 (그 투표의 가치를 알고있다), 그래서 난이 모델 생성 :

class Voto 
    include Mongoid::Document 
    embedded_in :picture 
    embedded_in :user 
    field :value, :type => Integer 
end 

class Picture 
    include Mongoid::Document 
    embeds_many :votos 
    embeds_many :comments 
    belongs_to :user 
    ... 
    ... 
end 

class User 
    include Mongoid::Document 
    ... 
    ... 
    has_many :pictures 
    embeds_many :votos 
end 

을하지만 난 그렇게하지 이것이 올바른지 아십시오. ¿ 동일한 모델 (이 경우 Voto)을 두 개의 다른 문서 (그림 및 사용자)에 저장할 수 있습니까?

어떻게하면 좋을까요?

답변

0

당신은 다형성 플래그 를 사용하여이 작업을 수행 할 수

class Voto 
    include Mongoid::Document 
    embedded_in :voteable, , polymorphic: true 
    field :value, :type => Integer 
end 

class Picture 
    include Mongoid::Document 
    embeds_many :votos, as: :voteable 
    embeds_many :comments 
    belongs_to :user 
    ... 
    ... 
end 

class User 
    include Mongoid::Document 
    ... 
    ... 
    has_many :pictures 
    embeds_many :votos, as: :voteable 
end 
( http://mongoid.org/en/mongoid/docs/relations.html#common 다형성 참조)