FactoryGirl - 如何在运行测试之前在数据库中生成类别条目的层次结构?
我正在建立一个电子商务网站,其核心是一个目录模块 - 它包含大约 20 个类别,每个类别有 5 至 5 个类别。 30个子类别,然后将产品与子类别相关联。
我正在尝试弄清楚如何创建父类别和父类别。然后至少有 2 个子类别与使用 FactoryGirl 的同一父类别关联。
这是我到目前为止所得到的:
Factory.define :parent_category do |f|
f.name "MetalWork"
end
Factory.define :child_category do |f|
f.name "Wedling"
f.association :parent_category
f.metatitle ""
f.metadescription ""
end
我正在考虑使用一个序列来生成子类别名称和子类别名称。 SEO 数据,但是如果我随后使用以下方法生成 2 个 child_category 模型:
FactoryGirl.build_list(:child_category, 2)
它将创建两个单独的父类别,这不是我想要的。
至少有两个子类别很重要,因为我们需要确保与子类别模型一起保存的各种 SEO 数据都显示在正确的页面上。
有谁知道如何做到这一点?我知道这可能是一个非常新手的问题,感谢您的帮助!
I am building an ecommerce website, the core of which is a catalogue module - it contains about 20 categories each of which has between 5 & 30 subcategories, products are then associated with subcategories.
I am trying to figure out how to create a parent category & then at least 2 subcategories which are associated with the same parent using FactoryGirl.
Here is what I have so far:
Factory.define :parent_category do |f|
f.name "MetalWork"
end
Factory.define :child_category do |f|
f.name "Wedling"
f.association :parent_category
f.metatitle ""
f.metadescription ""
end
I am thinking of using a sequence to generate the child category name & SEO data, however if I then generate 2 child_category models using:
FactoryGirl.build_list(:child_category, 2)
it will create two separate parent categories, which is not what I want.
Its important that there are at least two child categories because we need to ensure that various SEO data being saved with the child category models displays both on the correct pages.
Does anyone know how to accomplish this? I know this is probably a very newbie question, thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将哈希值传递给任何工厂调用以覆盖任何属性 - 这也适用于
build_list
调用:请参阅 文档。
You can pass a hash to any factory call to override any of the attributes - that goes for the
build_list
call too:See the examples in the section "Building and creating multiple records" in the docs.
我会从类似的事情开始
I'd start with something like