Search code examples
ruby-on-railsrubypaperclipdatabase-migration

Rails migration complains about undefined method `attachment' using paperclip


Lemmie just preface this by saying I'm fairly new to Rails.

Our app uses paperclip (3.2.4) to manage attachments and as usual, I generated a migration that looks something like:

class AddAttachmentPhotoToPhpfoxUsers < ActiveRecord::Migration
  def self.up
    change_table :phpfox_user do |t|
      t.attachment :photo
    end
  end

  def self.down
    drop_attached_file :phpfox_user, :photo
  end
end

(It's called phpfox_user because we have to build on top of a legacy db)

That's all great, works fine. However, we have to manage 2 databases and migrations to them as well, so I rearranged the migrations according to the suggestions on this post:

http://excid3.com/blog/rails-activerecord-multiple-databases-and-migrations

I don't know how good this is supposed to be, but it seems to a fairly neat solution and it organises the migrations quite well.

However now the paperclip migration doesn't work as it can't find the attachment type. I'm assuming it's no longer in scope or hasn't bound to the table object. Does anyone have any idea what I should do to bring it in, I've tried adding require 'paperclip' to the module but that doesn't help.

I've also tried using the add_attachment helper and that's also not found.

We're using Rails 3.2.13 and Ruby 2.0.0.

edit: typo


Solution

  • Ok, figured it out. The add_attachment helper is defined in the schema.rb file. Don't know if it's right or not but if I include:

    include Paperclip::Schema
    

    into the file, it works.