I just shipped some new features from my dev/test to heroku production. I have implemented the possibility to add comments to microposts via letting the micropost model selfjoin via a parent_id.
In dev when I use the functionality in dev it works fine, the console says the following for production:
2015-10-24T17:59:54.974585+00:00 app[web.1]: Started POST "/microposts" for 91.35.226.128 at 2015-10-24 17:59:54 +0000
2015-10-24T17:59:54.979271+00:00 app[web.1]: Car Load (1.0ms) SELECT "cars".* FROM "cars" WHERE "cars"."id" = $1 LIMIT 1 [["id", 1]]
2015-10-24T17:59:54.981868+00:00 app[web.1]:
2015-10-24T17:59:54.981869+00:00 app[web.1]: NoMethodError (undefined method `parent_id=' for #<Micropost:0x007f9f3036b290>):
2015-10-24T17:59:54.981870+00:00 app[web.1]: app/controllers/microposts_controller.rb:11:in `create'
2015-10-24T17:59:54.981871+00:00 app[web.1]:
2015-10-24T17:59:54.981871+00:00 app[web.1]:
2015-10-24T17:59:54.976228+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"*", "micropost"=>{"content"=>"Oooooder???"}, "parent_id"=>"37", "car_id"=>"2"}
The respective code in that it is complaining about is the last line in here:
def create
@micropost = current_users_car.microposts.build(micropost_params)
if !params[:id].nil?
@micropost.car_id = params[:id]
end
@micropost.user_id = current_user.id
@micropost.parent_id = params[:parent_id]
Since the line above it works, I presume that @micropost is not nil. I have tried in the heroku console to play around and create a micropost object and set and get the parent_id where it worked totally fine. So what is wrong here? I am lost :(
Since it worked in the console on production, the changes definitely made it to production. That likely means that the rails app needs to be restarted.
heroku restart -a app_name
You may have noticed that sometimes when you've pushed code to production and never restarted the service, that things have still worked! That because rails has an autoload feature that reloads certain code automatically. However it only reloads under certain conditions.
In this article (though a bit dated), reloading and how it works is explained.
In this case, while the schema was updated, the code wasn't reloaded automatically, so the server had to be manually restarted.
When deploying, always run migration and restart the server. You'll avoid situations like this.