문제

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