在Rails 3.2上创建链式种子

时间:2016-04-16 20:47:09

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我在这里有一个问题,我正在链接性别+类别+子类别的产品种子。

种子

g = Gender.create(:gender => 'Man')
g.categories.create(:name => "Clothes")


g = Gender.create(:gender => 'Woman')
g.categories.create(:name => "Accessories")


m = Category.find_by_name("Clothes")
m.subcategories.create(:name => "T-shirts" )


f = Category.find_by_name("Accessories")
f.subcategories.create(:name => "Rings" )

模型关系

性别

  • has_many:categories has_many:subcategories,through::categories

  • accepts_nested_attributes_for:categories,:subcategories

  • attr_accessible:gender,:categories_attributes,
    :subcategories_attributes

    分类

  • belongs_to:性别

  • has_many:subcategories

  • accepts_nested_attributes_for:subcategories

  • attr_accessible:name,:gender_id

子类别

  • belongs_to:categories
  • attr_accessible:name,:category_id

问题是:每次创建种子时,都会在Man子类别表上创建Woman子类别。有人可以帮忙吗?

感谢' S

0 个答案:

没有答案