In this table:
create_table "peripherals", id: :serial, force: :cascade do |t|
t.string "label", limit: 280
end
I limit the label field to 280 characters. I am using PostgreSQL as my database.
This record is saved to the database with no exceptions raised, even though it is longer than 280 characters:
Peripheral.create!(label: ("X" * 700))
Why is my limit of 280 not enforced?
I just tested this and the DB validation worked, but its good practice with ruby on rails to remove this validation and to move it into the model. from the model, this becomes much easier to find and update as well you have a range of other things that you can do other than just check for a maximum
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 about validates-length-of
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With