0

私はjson-documentsとrubyクラスを持っています

productjson-ドキュメント:

{
  "taxon_id": "5281bbed3aa823f9439dc02d",
  "name": "Mongoid in Action",
  "seo":
  {
    "permalink": "mongoid-in-action",
    "metadata":
    {
      "description": "Great programming book by smarter authors",
      "keywords": ["mongoid", "book", "action"],
      "title": "Programming book: Mongoid in Action"
    }
  }
}

taxonjson-ドキュメント:

{
  "_id": "5281bbed3aa823f9439dc02d",
  "name": "Programming",
  "seo":
  {
    "permalink": "programming",
    "metadata":
    {
      "description": "Catalogue for programming books",
      "keywords": ["programming", "coding", "hacking"],
      "title": "Programming books"
    }
  }
}

documents/matadata.rbファイル:

class Documents::Metadata
  include Mongoid::Document

  field :description, type: String
  field :title,       type: String
  field :keywords,    type: Array,  default: []

  embedded_in :seo
end

documents/seo.rbファイル:

class Documents::Seo
  include Mongoid::Document

  field :permalink, type: String

  embeds_one :metadata
  embedded_in :product
  embedded_in :taxon

  accepts_nested_attributes_for :metadata
end

documents/product.rbファイル:

class Documents::Product
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  embeds_one :seo
  belongs_to :taxon

  accepts_nested_attributes_for :seo
end

documents/taxon.rbファイル:

class Documents::Taxon
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  embeds_one :seo
  has_many :products

  accepts_nested_attributes_for :seo
end

私は一般的な seo json-document を持っており、別のドキュメント間で共有したいと考えています。それは正しい分割ですか?json-schema は適していますか?

4

1 に答える 1

0

レールはモデル名でリレーションを解決するため、embeds_one や embedded_in などの行にクラス名が記載されているため、すべてのモデルで 1 つの変更を修正するだけで正しいようembeds_one :seoに見えます。このように入れます

 embeds_one :seo, :class_name => "Documents::Seo"
于 2013-11-12T07:28:09.963 に答える