1

I want to implement a character limit to a data value on my PostgreSQL database for my Ruby on Rails application. Currently my database table looks like this:

create_table "applications", force: :cascade do |t|
    t.string   "name"
    t.string   "gender"
    t.date     "date_of_birth"
    t.string   "gpa"
    t.text     "essay"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
end

I want to change it so the "essay" only allows 10000 characters. From my limited understanding of PostgreSQL, text is unlimited by default whereas string is for 255 characters or less. I thought about implementing a Javascript conditional to not let the user hit the submission button in the client if the text is over 1,000 characters, but of course a tech savvy user could change that. What would be the most optimal way to do this?

1
  • Doesn't really matter, but did you mean 10000 or 1000? Commented May 9, 2016 at 12:06

1 Answer 1

1

Use rails model validations - validates_length_of.

On the rails end you can add this in application.rb file

validates_length_of :essay, :maximum => 1000

This will ensure that max size is not exceded. Also keep the javascript validation, since its helpful for all users except the malicious once.

Also I recommend you to change the table name from applications. You can get easily confused

Sign up to request clarification or add additional context in comments.

1 Comment

Model validations and database constraints are not interchangeable. Model validations can easily be by-passed if you use the proper method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.