I was making an original app and used the Boolean type for the first time. Write down the usage and precautions as a memorandum. The type of the column (column name: status, table name: suggestions) used for status judgment is boolean.
Development environment ruby 2.6.5 Rails 6.0.3.4
Set the column type to "boolean". Enforce a NOT NULL constraint with null: false to disallow empty columns.
2020***********_create_○○.rb
create_table :suggestions do |t|
t.boolean :status, null: false
t.timestamps
end
Describe the validation in the model. Note that the description of column and empty validation is different.
○○.rb
validates :text, presence: true #<=Description of other columns
validates :status, inclusion: { in: [true, false] }
Save as true or false.
@suggestion.status = true
Reference page How to validate boolean column in Rails and return it as an error if the type is different that's all
Recommended Posts