Вопрос

I tried these (tried in model file and in controller file):

record1 = Model.new("something", "something2", "something3")
record1.save

Nothing happened.

Tried to create record in seeds.rb file:

Model.create!(name => "something")

Then I get error:

wrong number of arguments (3 for 0..1)

But my table has three columns...

Then I tried to roll back migration and enter inside migration file like this:

record1 = Model.new(name: "somethingk", name2: "something2", name3: "something3")
record1.save

It migrated, but still schema did not create any records. So what is the way to create sample records in rails? I'm using rails 4.

Это было полезно?

Решение

The right way is using the seeds (db/seeds.rb):

Model.create!({:name1 => "something", :name2 => "something"})

The argument must be an hash (which is a single argument), probably you wrote something different for mistake

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top