Can't migrate table with array

1.8k views Asked by At

When I try to migrate this:

class CreateTasks < ActiveRecord::Migration[5.0]
  def change
    create_table :tasks do |t|
      t.string :category
      t.string :text
      t.string :asset_name
      t.string :correct_answers, array:true, default: []
      t.string :wrong_answers, array:true, default: []
      t.integer :score

      t.timestamps
    end
  end
end

I get error:

== 20161217135738 CreateTasks: migrating ====================================== -- create_table(:tasks) rails aborted! StandardError: An error has occurred, this and all later migrations canceled:

can't quote Array

I am using rails 5 and mysql on development. Any idea how to solve that problem?

1

There are 1 answers

0
Aniket Tiwari On

If you want support all databases you must serialize the array in a String I am giving you an example

class Documents < ActiveRecord::Base
 serialize :share
end

class AddShareToDocuments < ActiveRecord::Migration
 def change
   add_column :documents, :share, :string, :default => []
 end 
end