carrierwave - rails 3.1- undefined method: image_will_change

♀尐吖头ヾ 提交于 2019-11-30 05:42:15

The OP comments that he fixed it, however there's no answer set so I thought I'd add one for people coming across this in the future, which included myself until I figured it out :)

undefined method `x_will_change!' for # happens if you forget to add a column in your model's db table. If you have a model User and a AvatarUploader, with the uploader mounted as in the Carrierwave docs:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

Then the error will read

undefined method `avatar_will_change!' for #<User:0x00...>

To fix it (based on this example) add a column in a migration run the following in the console:

rails g migration AddAvatarToUsers avatar:string

This will generate the following migration:

class AddAvatarToUsers < ActiveRecord::Migration
  def change
    add_column :users, :avatar, :string
  end
end

Then migrate to apply the change (again in the console):

rake db:migrate

I suppose that author just forgot to run:

rake db:migrate

ALso, if you met such error inside of your tests then you should run:

rake db:test:prepare

Also, for anyone getting this error on heroku, you need to run

heroku run rake db:migrate

and

heroku restart

in the terminal after adding/removing fields/tables from your database.

Kreek, this is obviously a minor oversight, as most people would have realized by now, you probably meant to run this command, as one should, outside the console, otherwise, one would get the following:

'NameError: undefined local variable or method `migrate' for main:Object'.

I had similar problem but mine was because I was copying and pasting codes and forgot to delete

mount_uploader :picture, PictureUploader

from my model which did not use pictures. Hope this help others in future who could not figure out what happened

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