Chris Gregorio
Chris Gregorio

Reputation: 169

How to create a database record with rails? - ActiveRecord::UnknownAttributeError

I'm having trouble creating a new log in the database. I recently upgraded from an outdated version of rails to the newest version so I'm not too sure what has changed. I've been fixing a lot of bugs with the upgrade, but I'm stumped on this one. It's complaining about not having a name attribute for factory, but when I open my Schema it appears to be there so I'm not sure where to go from here.

View

= form_tag factories_path, :multipart => true  do
    = label :factory, :name, 'Name'
    = text_field :factory, :name
    %br
    = label :factory, :poolMin, "Pool Min"
    = text_field :factory, :poolMin
    %br
    = label :factory, :poolMax, "Pool Max"
    = text_field :factory, :poolMax
    %br
    = submit_tag 'Create Factories'

Controller

def create
  @factory = Factory.new(user_params)
  @factory.save
  redirect_to factories_path
end
def user_params
  params.require(:factory).permit(:name, :poolMin, :poolMax)
end

Schema.rb

create_table "factories", :force => true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
    t.integer  "poolMin"
    t.integer  "poolMax"
end

Error:

Unknown attribute 'name' for Factory. Extracted source (around line #10):

def create
  @factory = Factory.new(user_params)
  @factory.save
  redirect_to factories_path
end

Request

Parameters:

{"utf8"=>"✓", "authenticity_token"=>"7MXAkLikF6Mm+rXd3YrI8W25i9/Q85Peqv2AWeDRKl3+aZZtWtgiKuh0SSghhMoTHvGYn    pnfldduKqd08SkAdw==",
 "factory"=>{"name"=>"f",
 "poolMin"=>"33",
 "poolMax"=>"44"},
 "commit"=>"Create Factories"}

Upvotes: 0

Views: 432

Answers (2)

aaron-coding
aaron-coding

Reputation: 2621

We can't totally tell the exactly problem, but I can tell you the debugging process I would take.

1) Open up Rails console and make sure that you can Factory.new(name: "blah", poolMin: 0, poolMax: 10). You may be dealing with a lower level problem than you think. The issue might have nothing to do with user_params, (which should be factory_params, btw) or your controller.

2) Once you've ruled that out, try each attribute, one at a time and see if it's only one of them that gives the error. If you make a request with only :name, do you still get unknown attribute error? if you try :poolmin alone, (should be pool_min for style "The Rails Way") does it work? etc. At that point you can know which it is and delve deeper into your database and see if there is a small typo or something there that isn't readily noticeable.

4) Use the Gem Better Errors, you can call "fail" in your controller and then drop into Better Errors console and run commands as if you were at the command line at the exact moment in the controller. Play around with that and see if it works.

5) Try re-writing the code and see if you don't run into the same exact bug when it's re-written.

6) Out of possible desperation at this point, if somehow everything above didn't work, you can try Active Record's other methods of creating a record in the Database. Factory.create(name: "blah", poolMin: 0, poolMax: 10), that will create the model and save it in one command. If you do it with a bang! (i.e. Factory.create!(...)) it will give you a proper error message. You can use save!, etc. Anyway, you should not be getting to this point anyway!

Hope this helps!

Upvotes: 2

Nitin
Nitin

Reputation: 7366

ActiveRecord::UnknownAttributeError Clearly indicates that you don't have mapping column in your database (name in your case).

But your Schema.rb suggest that you have name column present in you factories table. So try run rails db on console and cross check column present in your factories table.

Upvotes: 1

Related Questions