Pergunta

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...

Foi útil?

Solução 2

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.

Outras dicas

Try the following code by adding remaining items in array in db/seeds.rb file and run rake db:seed command. That generates expected records in items table.

['css','rubyonrails','java'].each do |item|

Item.find_or_create_by_name(:name => item)

end

Good luck.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top