我正在使用带有rails 3.1的MongoID。我想在我的数据库(开发和生产)中播种。我有一个嵌入了Feeds的Pages模型。对于每个页面为嵌入式Feed添加种子的最佳方法是什么?我可以轻松地为所有页面数据播种,而不是嵌入的源。请注意,我希望实际拥有这些页面/提要的真实唯一数据,而不仅仅是任意测试数据。谢谢!
page.rb(model)
...
embeds_many :feeds
feed.rb(model)
class Feed
include Mongoid::Document
field :source, :type => String
field :user, :type => String
embedded_in:page,:inverse_of => :饲料 端
分贝/ seeds.rb
Page.create(title: "Page 1", userID: "EMAIL@gmail.com", presentation: 'cards', customURL: 'testing1')
Page.create(title: "Page 2", userID: "EMAIL@gmail.com", presentation: 'cards', customURL: 'testing2')
Page.create(title: "Page 3", userID: "EMAIL@gmail.com", presentation: 'cards', customURL: 'testing3')
Page.create(title: "Page 4", userID: "EMAIL@gmail.com", presentation: 'cards', customURL: 'testing4')
Page.create(title: "Page 5", userID: "EMAIL@gmail.com", presentation: 'cards', customURL: 'testing5')
如何才能最好地在每个页面中嵌入一些Feed数据?非常感谢。
答案 0 :(得分:6)
Page.create(title: "blah", feeds: [
Feed.new(source: "blahblah", user: "me!"),
Feed.new(....),
Feed.new(.....),
])
我是如何在db:seed
中完成的,我甚至还有一些深层文档。
答案 1 :(得分:0)
您可以这样做:
(1..5).each do |i|
page = Page.create(title: "Page #{i}", userID: "EMAIL@gmail.com", presentation: 'cards', customURL: "testing#{i}")
3.times { Feed.create(page: page) }
end