Why is db:migrate failing when i try to add attachment fields for paperclip?
Asked Answered
F

3

8

I am trying to add two different attachment fields. The migration is failing wether i run it using bundler or without. (bundle exec rake db:migrate or just rake db:migrate).

==  AddDiagramToQuestion: migrating ===========================================
-- change_table(:questions)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `has_attached_file' for #<ActiveRecord::ConnectionAdapters::Table:0x0000012b003b20>
 /Users/kboon/Documents/workspace/quiztaker/db/migrate/20111213182927_add_diagram_to_question.rb:6:in `block in up'
 /Users/kboon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:244:in `change_table'

The migration looks like this:

class AddDiagramToAnswer < ActiveRecord::Migration
  def self.up
    change_table :answers do |t|
      t.has_attached_file :diagram
    end
  end

  def self.down
    drop_attached_file :answers, :diagram
  end
end

The model also references methods added by paperclip and the app runs fine so its not that paperclip isn't installed at all. I've even tried added require 'paperclip' to the migration but that didn't help at all.

Frontispiece answered 22/1, 2012 at 20:53 Comment(7)
is the paperclip gem in your Gemfile?Transcendentalistic
Yes, I should have said that explicitly. has_attached_file works just fine in my models tooFrontispiece
what version of paperclip u are using ?Printmaker
The error is saying 20111213182927_add_diagram_to_question.rb:6 but your posted migration is for answers rather than questions. Have you posted the correct migration file here?Dagenham
One way around it is using of explicit migration generated by rails g paperclip question diagram.Storer
I have two migrations, one that adds a diagram to question and one for answer but they are identical. I created the explicit migrations for now but I still wonder why my migration didn't have access to the helpers.Frontispiece
Does it help to have require "paperclip" in your migration file?Lid
P
11

The migration that was created for me doesn't use the t.has_attached_file terminology anymore, it actually adds the columns explicitly. The migration would be created by running:

rails generate paperclip Answer diagram

Check out the example here.

Pembroke answered 10/2, 2012 at 21:10 Comment(1)
This is what I ended up doing even though the documentation still uses the has_attached_file method.Frontispiece
R
2

This worked for me

def change
  create_table :some_table do |t|
    t.attachment :avatar
    t.timestamps
  end
end
Ruttish answered 13/9, 2014 at 19:3 Comment(0)
F
2

Migration file should be look like

class AddDiagramToAnswer < ActiveRecord::Migration
  def self.up
    add_attachment :answers, :diagram
  end

  def self.down
    remove_attachment :answers, :diagram
  end
end

or

class AddDiagramToAnswer < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.attachment :avatar
    end
  end
end

has_attached_file is used in model.rb(answer.rb in your app)

with rails 5

Fluorine answered 5/10, 2016 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.