开发者

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

开发者 https://www.devze.com 2023-04-04 04:04 出处:网络
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:

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 wha开发者_如何学JAVAt 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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号