0
在我的種子文件,我沿着線的東西:如何爲具有嵌套屬性的模型編寫工廠?
Product.create([
{
:description => "something something",
:goals =>
[
Goal.find_by_name("Healthy Living"),
Goal.find_by_name("Build Muscle")
],
:variations_attributes =>
[
{
:flavor => Flavor.find_by_flavor("Vanilla"),
:price => 49.99,
},
{
:flavor => Flavor.find_by_flavor("Chocolate"),
:price => 29.99,
}
]
}])
我我會去建廠模仿這個紀錄?我在github上爲「工廠女孩」閱讀了「入門」讀物,但我仍然無法創造出像這樣的更高級工廠。任何幫助,將不勝感激。謝謝。
的產品型號是這樣的:
class Product < ActiveRecord::Base
attr_accessible :active, :goal_id, :description, :gender_id, :name, :internal_product_id, :image, :image_cache, :brand, :variations
attr_protected nil
belongs_to :gender
has_many :variations, :dependent => :destroy
has_many :product_goals
has_many :goals, :through => :product_goals
accepts_nested_attributes_for :variations, :reject_if => lambda { |a| a[:price].blank? }, :allow_destroy => true
mount_uploader :image, ImageUploader
def flavors
# return the set of available flavors
# this method is necessary because flavors are associated
# with variations, not the product itself
self.variations.collect{|v| v.flavor}.uniq.sort_by{|f| f.flavor}
end
end
這工作,謝謝! – xxyyxx