Sebastian Zeki
Sebastian Zeki

Reputation: 6874

How to seed data using db:seed

So i have spent a while looking for the answer but perhaps I dont understand the principles. I have a table called user which has two fields called name and value. I have migrated this fine and created a model and controller as well.

My migration file looks like:

 class CreateUsers < ActiveRecord::Migration
   def change
     create_table :users do |t|
       t.string :name
       t.integer :value

       t.timestamps null: false
     end
   end
 end

I am using the following to seed the data

Users.create!([{ name: 'Chicago' , value:'12'}, { name: 'Coppo' , value:'15'} , { name: 'Bilbo' , value:'13' }, { name: 'Steve' , value:'12'}, { name: 'Shazza' , value:'11'}])

followed by

 rake db:seed

But I keep on getting the same error of:

 NameError: uninitialized constant Users

Upvotes: 1

Views: 188

Answers (1)

Sharvy Ahmed
Sharvy Ahmed

Reputation: 7405

You are missing Naming Conventions.
For a class User, you should have a database table called users.

Database Table - Plural (e.g., users, user_profiles).
Model Class - Singular with the first letter of each word capitalized (e.g., User, UserProfile).

Model actions would be:

User.create(Hash)
UserProfile.create(Hash)

Change your seed code into this:

User.create!([{ name: 'Chicago' , value:'12'}, { name: 'Coppo' , value:'15'} , { name: 'Bilbo' , value:'13' }, { name: 'Steve' , value:'12'}, { name: 'Shazza' , value:'11'}])

Upvotes: 2

Related Questions