Roman Kanafotskiy Roman Kanafotskiy - 1 year ago 151
Ruby Question

ActiveModel::MissingAttributeError (can't write unknown attribute `order_id`) in production

In development environment everything works fine but in production (when deployed to Heroku) it throws me MissingAttributeError.

class Order < ApplicationRecord
has_many :cart_items, dependent: :destroy

class CartItem < ApplicationRecord
belongs_to :order, optional: true, foreign_key: "order_id"

create_table "cart_items", force: :cascade do |t|
t.integer "item_id"
t.integer "cart_id"
t.integer "user_id"
t.integer "order_id"

Answer Source

when you migrate on localhost, the schema is built to reflect your local db state and it's possible for it to be un-synced from production. Perhaps you've changed something in a migration, but heroku is still working off the old version. Whether or not Heroku runs a migration is determied by the timestamp in the filename, not the content of the migration.

Basically, if you have a deployed app, don't change old migrations. If you don't mind destroying all the data in the production database, run heroku pg:reset DATABASE then rake db:migrate again. If you can't delete the data, there are still ways to fix the problem - see Rails rake db:migrate has no effect

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download