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

早过忘川 提交于 2020-01-01 05:31:11

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7373039/getting-nomethoderror-undefined-method-name-for-nilnilclass-when-creating-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!