0

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.

1
  • what exactly do you mean by schema didn't create records?You have to specify the names of the fields like you did in your last example.However after calling save on record1 it should save it to the database.Are you trying this in rails console?
    – Brock90
    Oct 28, 2013 at 16:46

1 Answer 1

0

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

3
  • I ran that and trace showed this: ** Invoke db:seed (first_time) ** Execute db:seed ** Invoke db:abort_if_pending_migrations (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:abort_if_pending_migrations Also schema didn't change. So how can I check if record was created?
    – Andrius
    Oct 28, 2013 at 16:55
  • So I think schema do not show if records were created. I checked in console and it really created it. It looks this is working. Thanks
    – Andrius
    Oct 28, 2013 at 17:01
  • Yes it doesn't; you can count them at the and of the script (something like created #{Model.count} records, I use it all the time
    – mdesantis
    Oct 28, 2013 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.