0

たとえば、Product と Brand という 2 つのモデルを考えた場合、各モデルで関連するドキュメントの ID ごとにフィールドを宣言する必要がありますか?

class Product
  include Mongoid::Document

  field :name, :type => String
  field :brand_id, :type => Moped::BSON::ObjectId

  validates :name, presence: true
  validates :brand_id, presence: true

  belongs_to :brand
end

--

class Brand
  include Mongoid::Document
  field :name, :type => String
  field :description, :type => String
  field :product_id, :type => Moped::BSON::ObjectId

  validates :name, presence: true, uniqueness: true
  validates :product_id, presence:true

  has_many :products
end

フィールドbrand_idとフィールドproduct_idは、モデルがそれらの存在を宣言しているかどうかに関係なく、両方とも自動的に作成されるため、コードはそれらが含まれているかどうかに関係なく機能します。この種の標準的な慣行が何であるかに興味があります。

4

1 に答える 1

0

何か問題があると思います...

クラス Brand では、*has_many* 関係がある場合、'product_id' を使用しないでください。

クラス商品OKです。

于 2013-07-10T07:07:03.933 に答える