简体   繁体   中英

Best way to seed content text in rails 4

I know how to create a standard seed file in rails and seed my database. However, I have a wysiwyg editor that I used to create many pages of content. The content field is a "text" type and contains a full page of HTML. I will be exporting this data out and want to create a seed file with it.

Any ideas on the best way to create my seed files?

For example, should I create a reference the content from separate files? or should I just keep the text in one seed file? Would it be possible to show an example of a recommendation?

You can have a file containing the content and then just reading it and seeding your db:

file_content = File.read('path to file with extension');
MyModel.create(text: file_content);

And of course, if you have multiple items you need to seed, just loop over them with the right file name or by all file in certain directory.

Another option if you have multiple attributes and the data is already in the DB, you can use seed_dump gem: https://github.com/rroblak/seed_dump

so given an existing DB, running

rake db:seed:dump

will produce the following in the seed.rb :

Product.create!([
  { category_id: 1, description: "Long Sleeve Shirt", name: "Long Sleeve Shirt" },
  { category_id: 3, description: "Plain White Tee Shirt", name: "Plain T-Shirt" }
])
User.create!([
  { password: "123456", username: "test_1" },
  { password: "234567", username: "test_2" }
])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM