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 模式是否合适?

4

1 回答 1

0

它看起来是正确的,只是纠正了所有模型中的一个更改,对于像 embeds_one 和 embedded_in 这样的行提到了类名,因为 rails 通过模型名解析关系就像如果你把embeds_one :seo它寻找类名 SEO 而不是 Documents::Seo。像这样放

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