我正在尝试添加两个不同的附件字段。迁移失败了我使用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',但这根本没有帮助。
答案 0 :(得分:10)
为我创建的迁移不再使用t.has_attached_file术语,它实际上是显式添加列。将通过运行:
创建迁移rails generate paperclip Answer diagram
查看示例here。
答案 1 :(得分:2)
这对我有用
def change
create_table :some_table do |t|
t.attachment :avatar
t.timestamps
end
end
答案 2 :(得分: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)
使用rails 5