2

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

dochead
  • 1,785
  • 2
  • 18
  • 20
  • Switch the db: http://stackoverflow.com/questions/1404620/using-rails-migration-on-different-database-than-standard-production-or-devel – DeveloperX Jul 05 '13 at 09:09
  • I'm not sure how that would help? I'd specifically like to run the migrations automatically on all environments and not have to manually switch databases. – dochead Jul 05 '13 at 12:29

3 Answers3

5

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.

dochead
  • 1,785
  • 2
  • 18
  • 20
  • I added that line into my migration file and it worked. Thanks, I've been searching for this solution everywhere! – furman87 Feb 11 '14 at 02:36
1

Make sure paperclip (possibly latest version) is present in your Gemfile, run bundle install and then run

bundle exec rake db:migrate

It should just work.

Rishav Rastogi
  • 15,484
  • 3
  • 42
  • 47
  • No, I've tried that a few times. The gem is installed fine and it works, it's just that the paperclip doesn't seem to attach to the migration file if it's in a subdirectory within db/migrations. – dochead Jul 05 '13 at 12:27
1

I am using ruby 2.1.5 and rails 4.2.1

I pulled code from git after my partner added paperclip gem

I downloaded paperclip gem, added to my gemfile (our gemfiles are different), but did not specify version of paperclip.

So I was surprised that I had to use 'has_attached_file' (which is for older versions of paperclip)

I had to:

1)include paperclip::Schema in schema.rb 2)replace attachment with has_attached_file 3)rake db:migrate