gpt4 book ai didi

ruby-on-rails - Rails3/Mongoid - 基本数据库 :seed with embedded documents

转载 作者:可可西里 更新时间:2023-11-01 09:11:51 25 4
gpt4 key购买 nike

我在 Rails 3.1 中使用 MongoID。我想播种我的数据库(在开发和生产中)。我有一个嵌入了 Feed 的页面模型。为每个页面植入嵌入式提要的最佳方式是什么?我可以轻松地为所有页面数据播种,而不是嵌入的提要。请注意,我正在寻找这些页面/提要的实际唯一数据,而不仅仅是任意测试数据。谢谢!

page.rb(模型)

...
embeds_many :feeds

feed.rb(模型)

class Feed
include Mongoid::Document
field :source, :type => String
field :user, :type => String

embedded_in :page, :inverse_of => :feeds结束

db/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')

我怎样才能最好地在每个页面中嵌入一些提要数据?非常感谢。

最佳答案

Page.create(title: "blah", feeds: [
Feed.new(source: "blahblah", user: "me!"),
Feed.new(....),
Feed.new(.....),
])

我在 db:seed 中就是这样做的,我什至有一些是多个文档深度。

关于ruby-on-rails - Rails3/Mongoid - 基本数据库 :seed with embedded documents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7721591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com