Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 418
Kyle Boon Avatar asked Jan 22 '12 20:01

Kyle Boon


People also ask

What is rake db migrate?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

Can you edit a migration file Rails?

If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate.

What is paperclip in Rails?

Paperclip is an easy file attachment library for Rails Applications. Attached files are saved to the file system, database or cloud and referenced in the browser by an easily understandable specification. Here is an example to explain the image attachment for a user profile in an application.


3 Answers

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.

like image 161
andrew.rockwell Avatar answered Sep 26 '22 00:09

andrew.rockwell


This worked for me

def change
  create_table :some_table do |t|
    t.attachment :avatar
    t.timestamps
  end
end
like image 41
jmosesman Avatar answered Sep 22 '22 00:09

jmosesman


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

like image 25
Nyein Avatar answered Sep 25 '22 00:09

Nyein