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