Ruby on Rails id column not generated after db:migration -


i'm following course on coursera , i'm on first assignment details of can found on this , this link. when ran rspec found test cases failing, turned out schema didn't have id column in it. in course said when run migration id column generated automatically created_at , updated_at. has idea why id column didn't generated. know can overcome problem specifying in new migration wanted know reason.

here's schema have:

activerecord::schema.define(version: 20161108162529)    create_table "profiles", force: :cascade |t|     t.string   "gender"     t.integer  "birth_year"     t.string   "first_name"     t.string   "last_name"     t.datetime "created_at", null: false     t.datetime "updated_at", null: false   end    create_table "todo_items", force: :cascade |t|     t.date     "due_date"     t.string   "title"     t.text     "description"     t.boolean  "completed",   default: false     t.datetime "created_at",                  null: false     t.datetime "updated_at",                  null: false   end    create_table "todo_lists", force: :cascade |t|     t.string   "list_name"     t.date     "list_due_date"     t.datetime "created_at",    null: false     t.datetime "updated_at",    null: false   end    create_table "users", force: :cascade |t|     t.string   "username"     t.string   "password_digest"     t.datetime "created_at",      null: false     t.datetime "updated_at",      null: false   end  end 

this migrations todolists:

class createtodolists < activerecord::migration   def change     create_table :todo_lists |t|       t.string :list_name       t.date :list_due_date        t.timestamps null: false     end   end end 

the model class generated:

class todolist < activerecord::base end 

my method inserting record in in assignment.rb file asked:

 def create_todolist(params)       # accept hash of todolist properties (`:name` , `:due_date`) input parameter. note these not 100% same model class.       # use todolist model class create new user in db       # return instance of class primary key (`id`), , dates (`created_at` , `updated_at`) assigned       t1 = todolist.new       t1.list_name = params["name"]       t1.list_due_date = params["due_date"]       t1.save       todolist.first   end 

rspec code failing:

context "rq03.2 assignment code has create_todolist method"         { is_expected.to respond_to(:create_todolist) }          "should create_todolist provided parameters"             expect(todolist.find_by list_name: "mylist").to be_nil             due_date=date.today             assignment.create_todolist(:name=> 'mylist', :due_date=>due_date)             testlist = todolist.find_by list_name: 'mylist'             expect(testlist.id).not_to be_nil             expect(testlist.list_name).to eq "mylist"             expect(testlist.list_due_date).to eq due_date             expect(testlist.created_at).not_to be_nil             expect(testlist.updated_at).not_to be_nil         end   

actual message when fails test:

failures:

1) assignment rq03 rq03.2 assignment code has create_todolist method should create_todolist provided parameters      failure/error: expect(testlist.id).not_to be_nil       nomethoderror:        undefined method `id' nil:nilclass      # ./spec/assignment_spec.rb:173:in `block (4 levels) in <top (required)>'      # ./spec/assignment_spec.rb:14:in `block (2 levels) in <top (required)>' 

the schema won't show id column. double checked 1 of rails apps:

db/schema.rb

create_table "users", force: :cascade |t|   t.string   "email",                  default: "",    null: false   t.string   "first_name"   t.string   "last_name" end 

and when describe table in postgres \d users, see id column:

         column         |            type             |                     modifiers ------------------------+-----------------------------+----------------------------------------------------  id                     | integer                     | not null default nextval('users_id_seq'::regclass)  email                  | character varying           | not null default ''::character varying  first_name             | character varying           |  last_name              | character varying           | 

if there reason don't want id, can omit passing id: false create_table.

create_table :users, id: false |t|   #... end 

probably horrible idea on users table :-)


update

looks not creating new todolist.

the clue here in error: "undefined method 'id' nil:nilclass". testlist nil.

one tip use activerecord's "bang" methods in tests. cause them throw exception if fail. super helpful when trying track down errors -- you'll find place actual error , not side effect down line.

i bet can update create_todolist track down

def create_todolist(params)   # updated: use activerecord create! method.    # throw exception if fails.   todolist.create!(list_name: params[:name], due_date: params[:due_date]) end 

i update spec, changing:

  testlist = todolist.find_by list_name: 'mylist' 

to use bang:

  testlist = todolist.find_by! list_name: 'mylist' 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -