Search code examples
ruby-on-railsruby-on-rails-4scaffoldingseed

Populate a Scaffold via Seed


I have an Item's Scaffold which has approximately 200+ Item's which are pre-fix.

So adding them manually is really painful.

Can i populate the Scaffold in my seed-file and then db:seed on production ?

I don't know the proper method but i think it's something like this:

items = Item.create([
    { name: 'css' }, 
    { name: 'css3' },
    { name: 'ruby' },
    { name: 'rails' },
    { name: 'python' },
    { name: 'html' }
    ])

I'm searching for a Solution that will seed my Item's Scaffold...


Solution

  • You have to do it that way, but you don't need to asign it to the items variable.

    You just have to add to the seed.rb file the code with your items. I've added the delete_all before to avoid creating duplicate items.

    Item.delete_all
    Item.create([
    { name: 'css' }, 
    { name: 'css3' },
    { name: 'ruby' },
    { name: 'rails' },
    { name: 'python' },
    { name: 'html' }
    ])
    

    And then you'll have to seed your database with rake:db seed.