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