`length:` doesn't apply to row column with PostgreSQL?
In this table:
create_table "peripherals", id: :serial, force: :cascade do |t|
t.string "label", limit: 280
end
I am limiting the field to label
280 characters. I am using PostgreSQL as my database.
This entry is stored in the database without any exception, even if it is longer than 280 characters:
Peripheral.create!(label: ("X" * 700))
Why doesn't my 280 limit apply?
+3
source to share
1 answer
I just tested this and the DB validation worked, but its good practice with ruby ββon rails to remove this validation and move it into the model. from the model it becomes much easier to find and update, and you also have a number of other things you can do besides just checking the
class Peripheral < ApplicationRecord
validates_length_of :label, :maximum => 280 # there shouls be no more than 280 characters
#validates_length_of :label, :minimum => 2 # there shouls be no less than 2 characters
#validates_length_of :label, :in => 2..280 # the number of character should be between 2 and 280
#validates_length_of :label, :is => 280 # The number of character should be exacly 280
end
you can find more details on validates-length-of
+2
source to share