Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NoMethodError (undefined method `name' for nil:NilClass) when creating a new model in Heroku console

I just did a push to Heroku and tried doing some testing by adding a model through rails_admin. When I did that I got a generic error page. I went into the logs and noticed this message:

NoMethodError (undefined method `name' for nil:NilClass)

I then opened heroku console and tried adding the model manually and received the same message when trying to save.

NoMethodError: undefined method `name' for nil:NilClass

Here is the model:

class Board < ActiveRecord::Base
  attr_accessible :name, :description

  validates :name, :presence => true
  validates :description, :presence => true
  validates_uniqueness_of :name, :case_sensitive => false

  has_many :subjects
  scope :hidden, where(:is_hidden => true)
  scope :visible, where(:is_hidden => false)
end

Any ideas what might be happening with this - or where to start looking?

I did the migration and was able to see that it recognized the model and it's attributes when working in the console.

Thanks!

like image 362
someoneinomaha Avatar asked Sep 10 '11 16:09

someoneinomaha


People also ask

What does it mean when you get this error message undefined method user for NILClass?

This is a common Ruby error which indicates that the method or attribute for an object you are trying to call on an object has not been defined.

What is nil NILClass?

The Undefined method for nil:NILClass occurs when you attempt to use a formula on a blank datapill. This indicates that the datapill was not provided any value at runtime.

What is an undefined method in Ruby?

The undefined method is also called the NoMethodError exception, and it's the most common error within projects, according to The 2022 Airbrake Error Data Report. It occurs when a receiver (an object) receives a method that does not exist.


2 Answers

After being bitten by this issue twice, I asked heroku support team for the reason why it happens. And they replied:

"after running rake db:migrate, you have to restart your application so it can pick up the schema changes, since the schema info is cached during boot in production mode."

So always remember to restart heroku app by 'heroku restart' after running a new migration.

like image 66
Zeeshan Avatar answered Oct 05 '22 13:10

Zeeshan


I'm not sure if there is a delay or something with Heroku when pushing changes out and running db:migrate but after spending a few hours out and about, I came back, ran rake db:migrate again, which appeared to do nothing and then I tried creating the model again and it worked without any problems.

So all seems well now, but I can't tell I just needed to wait longer before testing with Heroku - or whether running the migration again actually did something.

like image 26
someoneinomaha Avatar answered Oct 05 '22 13:10

someoneinomaha