Monkeypatch a model in a rake task to use a method provided by a plugin?

Posted by gduquesnay.mp on Stack Overflow See other posts from Stack Overflow or by gduquesnay.mp
Published on 2010-04-17T12:22:54Z Indexed on 2010/04/17 12:33 UTC
Read the original article Hit count: 346

Filed under:
|
|
|

During some recent refactoring we changed how our user avatars are stored not realizing that once deployed it would affect all the existing users. So now I'm trying to write a rake task to fix this by doing something like this.

namespace :fix do

  desc "Create associated ImageAttachment using data in the Users photo fields"
  task :user_avatars => :environment do
    class User      
      # Paperclip
      has_attached_file :photo ... <paperclip stuff, styles etc>
    end

    User.all.each do |user|
      i = ImageAttachment.new
      i.photo_url = user.photo.url
      user.image_attachments << i
    end    
  end

end

When I try running that though I'm getting undefined method `has_attached_file' for User:Class

I'm able to do this in script/console but it seems like it can't find the paperclip plugin's methods from a rake task.

© Stack Overflow or respective owner

Related posts about rails

Related posts about rake