3
埋め込みドキュメントを追加しようとしています。私はこの構文が間違っていることを知っていますが、私が達成しようとしていることを実証しています。さまざまなフィールドに同じ種類の埋め込みドキュメントがあります
class Email
include Mongoid::Document
embeds_many :recipients, as: :to
embeds_many :recipients, as: :cc
embeds_many :recipients, as: :bcc
field :from, type: String
field :subject, type: String
field :body, 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
以下のコードサンプルは動作しますが、コードの重複を避けるようにしています。
class Email
include Mongoid::Document
embeds_many :to_recipients
embeds_many :cc_recipients
embeds_many :bcc_recipients
field :from, type: String
field :subject, type: String
field :body, type: String
end
class ToRecipient
include Mongoid::Document
field :email_address, type: String
field :name, type: String
validates :email_address, :presence => true
embedded_in :emails
end
class CcRecipient
include Mongoid::Document
field :email_address, type: String
field :name, type: String
validates :email_address, :presence => true
embedded_in :emails
end
class BccRecipient
include Mongoid::Document
field :email_address, type: String
field :name, type: String
validates :email_address, :presence => true
embedded_in :emails
end
誰もがこれを処理するきれいな方法を知っていますか?