Pregunta

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

¿Fue útil?

Solución 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.

Otros consejos

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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top