Rails3 / Mongoid - basic db: seed with embedded documents

I am using MongoID with rails 3.1. and I would like to sow my database (both in dev and in production). I have a Pages model with feeds enabled. What is the best way for me to plant embedded channels for each page? I can easily sow all the data on the page, not the built-in feeds. Please note that I am looking for actual unique data for these pages / channels, not just arbitrary test data. thanks!

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 =>: channels end

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

What's the best way to embed some feed data on each page? Thank you very much.

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

As I do this in my db:seed , I even have several documents with a deep document.

+6
source

You can do something like this:

 (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 
0
source

All Articles