简体   繁体   中英

Rails migration of an array inside a model doesn't work

I am trying to create a model with some arrays inside it. After creating the migration file by this command:

rails g model post poster_first_name:string poster_first_name:string email:string poster_id:integer poster_profile_pic:string message:string post_type:string comments:text

The migration file is created and I modify the migration file to look like this (trying to make the post_type and comments arrays):

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.integer :poster_id,        null: false, default: 0
      t.string :poster_first_name,              default: ""
      t.string :poster_last_name,               default: ""
      t.string :poster_profile_pic,             default: ""
      t.string :message,           null: false, default: ""
      t.string :post_type,         array: true, default: []
      t.text :comments,            array: true, default: []

      t.timestamps null: false
    end
  end
end

This migration file looks correct. After I run rake db:migrate

This is how my schema looks:

 create_table "posts", force: :cascade do |t|
   t.integer  "poster_id",          default: 0,          null: false
   t.string   "poster_first_name",  default: ""
   t.string   "poster_last_name",   default: ""
   t.string   "poster_profile_pic", default: ""
   t.string   "message",            default: "",         null: false
   t.string   "post_type",          default: "--- []\n"
   t.text     "comments",           default: "--- []\n"
   t.datetime "created_at",                              null: false
   t.datetime "updated_at",                              null: false
 end

So basically rails doesn't understand that the fields are arrays. I have tried a lot of different stuff. But no luck. I would appreciate any help. (This might be a noob question)

Please check this answer: Ruby on Rails: Submitting an array in a form

So you can understand how to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM