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

送分小仙女□ 提交于 2020-01-07 02:52:09

问题



I am trying to add a multiple image uploader with Carrierwave following the documentation to my app.

Normally my schema.rb looks like this

  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

And when I run :

rails g migration add_images_to_progresses images:json and rake db:migrate

My schema change and display that weird thing.... Is it a problem with Sqlite3 or Pg? What should I do ?

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

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

end

回答1:


I tried using the same code for both SQLite and PostgreSql. In SQLite there is no datatype "JSON" available, and so it throws an error. When I tried the same in PostgreSQL, it worked.

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

If you want to use the SQLite then you will have to generate a string from your JSON and then save that string in your database as a regular string.

I prefer to use PostgreSQL, which supports JSON datatype.



来源:https://stackoverflow.com/questions/38948066/schema-rb-display-could-not-dump-table-progresses-because-of-following-nometho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!