2012-04-16 4 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 
関連する問題