2012-04-16 8 views
-1

처음으로 몽고 이드를 사용하고 있습니다. 제목, 본문 및 to, cc 및 bcc 수신자의 배열이 포함 된 전자 메일 모음을 저장하고 싶습니다. 예 :몽고 이드 문서 작성

{to: [{email: '[email protected]', name: 'Andrew'}], cc: ... 

그러나, 나는 Mongoid를 사용하여이 데이터를 모델링하는 방법을 알아낼 수 없습니다. 나는이 용어를 임베디드 문서라고 부르지 만, 내가 시도한 모든 것이 제대로 작동하지 않는 것 같아. 몽고이드 모델을 올바르게 만들려면 어떻게해야합니까?

+0

정확히 무엇을 시도 했습니까? –

+0

문서를 읽었습니까? 이 작업을 매우 명확하게 수행하는 방법을 설명합니다. http://mongoid.org/docs/documents.html –

+0

이 링크는 더 좋습니다. http://mongoid.org/docs/relations/embedded/1-n.html –

답변

2

여기 해결책이 있습니다. 여러 필드에 대해 클래스를 다시 사용하려면 클래스 이름을 지정할 수 있습니다.

class Email 
    include Mongoid::Document 

    embeds_many :to_recipients, :class_name => "Recipient" 
    embeds_many :cc_recipients, :class_name => "Recipient" 
    embeds_many :bcc_recipients, :class_name => "Recipient"  
    embeds_one :from, :class_name => "Recipient" 

    field :subject, type: String 
    field :body_text, type: String 
    field :body_html, type: String 
end 

class Recipient 
    include Mongoid::Document 
    field :email_address, type: String 
    field :name, type: String 
    validates :email_address, :presence => true 
    embedded_in :emails 
end