当我尝试为回形针添加附件字段时,为什么db:migrate失败?

问题描述 投票:8回答:3

我想添加两个不同的附件字段。迁移失败了我使用bundler或不使用bundler运行它。 (bundle exec rake db:migrate或者只是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'

迁移看起来像这样:

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

该模型还引用了回形针添加的方法,应用程序运行正常,因此根本没有安装回形针。我甚至尝试在迁移中添加了require'paperclip',但这根本没有帮助。

ruby-on-rails ruby-on-rails-3.1 paperclip
3个回答
11
投票

为我创建的迁移不再使用t.has_attached_file术语,它实际上显式添加了列。将通过运行以下命令创建迁移:

rails generate paperclip Answer diagram

查看示例here


2
投票

这对我有用

def change
  create_table :some_table do |t|
    t.attachment :avatar
    t.timestamps
  end
end

1
投票

迁移文件应该是这样的

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

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

要么

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

has_attached_file用于model.rb(应用程序中的answer.rb)

用铁轨5

© www.soinside.com 2019 - 2024. All rights reserved.