Reputation: 372
I don't know why, but I am stuck with a simple issue. I have a simple User model and I have given a simple validation to one of its field ie., username for presence and its length.
Below is the code:
user.rb
class User < ActiveRecord::Base
validates :username, presence: true, length: { minumum: 3, maximum: 25 }
end
Accordingly,
user = User.new(username: 'hi')
user.save
must fail as the length of the username (i.e., is too short as per the validation given). But in my case it is getting saved without any errors. Currently it is only giving errors when username is blank but not giving errors for min length. Note: I have reload! my rails console
Upvotes: 0
Views: 2813
Reputation: 493
You have misspelled minimum :) Try this:
class User < ActiveRecord::Base
validates :username, presence: true, length: { minimum: 3, maximum: 25 }
end
Also, re: the answer below, this is directly from the RailsGuides for Active Record Validations:
class Essay < ActiveRecord::Base
validates :content, length: {
minimum: 300,
maximum: 400,
tokenizer: lambda { |str| str.split(/\s+/) },
too_short: "must have at least %{count} words",
too_long: "must have at most %{count} words"
}
end
Upvotes: 5