Schema.rb display Could not dump table "progresses" because of following NoMethodError # undefined method `[]' for nil:NilClass

Schema.rb display Could not dump table "progresses" because of following NoMethodError # undefined method `[]' for nil:NilClass


我正在尝试将带有 Carrierwave following the documentation 的多图像上传器添加到我的应用程序。

通常我的schema.rb是这样的

  ActiveRecord::Schema.define(version: 20160814232416) do

   create_table "progresses", force: :cascade do |t|
     t.string   "title"
     t.string   "date"
     t.text     "content"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
    end
  end

当我 运行 :

rails g migration add_images_to_progresses images:jsonrake db:migrate

我的模式发生变化并显示那个奇怪的东西.... 是Sqlite3还是Pg的问题?我该怎么办?

ActiveRecord::Schema.define(version: 20160815005123) do

  # Could not dump table "progresses" because of following NoMethodError
  #undefined method `[]' for nil:NilClass

end

我尝试对 SQLite 和 PostgreSql 使用相同的代码。在 SQLite 中没有可用的数据类型 "JSON",因此它会抛出一个错误。当我在 PostgreSQL 中尝试相同的方法时,它起作用了。

ActiveRecord::Schema.define(version: 20160815070637) do 
  enable_extension "plpgsql"
  create_table "progresses", force: :cascade do |t|
    t.string   "title"
    t.string   "date"
    t.text     "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.json     "images"
  end

end

如果您想使用 SQLite,那么您必须从 JSON 生成一个字符串,然后将该字符串作为常规字符串保存在您的数据库中。

我更喜欢使用 PostgreSQL,它支持 JSON 数据类型。