Rails migration complains about undefined method `attachment' using paperclip

馋奶兔 提交于 2019-12-11 08:37:53

问题


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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/17482220/rails-migration-complains-about-undefined-method-attachment-using-paperclip

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