LoginSignup
2
0

More than 3 years have passed since last update.

is out of range for ActiveModel::Type::Integer with limit 4 bytesエラー(ActiveRecordのint型項目の最大値超過エラー)

Last updated at Posted at 2021-04-25

20210425-143446.png

ActiveRecordでinteger型の項目をマイグレして、int型の最大値以上の数を入れて保存しようとすると、モデル側で以下のエラーが出ます。

09077778888で電話番号に設定した数字が大きすぎるって怒られてます。

電話番号(ハイフンなし10桁)
/\A\d{10}\z/

携帯番号(ハイフンなし11桁)
/\A\d{11}\z/

携帯番号(ハイフンなし10桁or11桁)
/\A\d{10,11}\z/

さらにデータカラムの設定ミス

20210424083812_create_addresses.rb
class CreateAddresses < ActiveRecord::Migration[6.0]
  def change
    create_table :addresses do |t|
      t.string       :postal_code             ,null:false
      t.integer      :prefecture_id           ,null:false
      t.string       :town                    ,null:false
      t.string       :address                 ,null:false
      t.string       :building
      t.integer       :phone_number            ,null:false
      t.references   :purchase                ,null:false, foreign_key: true 
      t.timestamps
    end
  end
end

電話番号の欄がintegerになっていたので入りませんでした。

バリデーションの設定が数値設定になっていたので直したのですが、マイグレーションファイルも数値型になっていたのでどうしても保存か不可能でした。ファイグレーションファイルをロールバックして修正してのち再度、string型に変更して直りました。

2
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
2
0