0
0

More than 3 years have passed since last update.

[Rails]serializeなcolumnのdefault値を空の配列にしたい。

Last updated at Posted at 2020-01-17

やりたいこと

migration
class Users < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.integer  :id
      t.string   :hoges, null: false, default: []    # これ
    end
  end
end

でもこれじゃ

== 20200109052420 Users: migrating ============================
-- create_table(:search_histories)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

can't quote Array
/Users/lyrical_school/project/disappearing_planet/db/migrate/20200109052420_create_users.rb:3:in `change'
bin/rails:9:in `require'
bin/rails:9:in `<main>'

空配列を登録した時って何が保存されるんだっけ?

DB確認してみる。

--- []
が登録されてた。

じゃあそれをdefaultに設定してみる。

migration
class Users < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.integer  :id
      t.string   :hoges, null: false, default: '--- []'
    end
  end
end

完璧。

殴り書きだから後日ちゃんとまとめる。まとめないかも。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0