Mongoid のアソシエーションについて初心者向けの質問があります。私はこの2つのモデルを持っています
class Manufacturer
include Mongoid::Document
field :name, type: String
field :url, type: String
has_many :products
end
と
class Product
include Mongoid::Document
field :manufacturer_name, type :String
field :model, type: String
field :price, type: Float
belongs_to :manufacturer
end
次に、新しい会社を作成します。
man = Manufacturer.create name: 'Flower Power Companies', url: 'www.flowerpower.com'
そして新製品:
prod = Product.create manufacturer_name: what_comes_here, model: 'Foo0815', price: '19.90'
prod.manufacturer_name を man.name に参照するには? man.name が変更される場合、prod.manufacturer_name は自動的に変更されます。