I'm making a new web app using Rails, and was wondering, what's the difference between string and text? And when should each be used?
string
text
The difference relies in how the symbol is converted into its respective column type in query language.
with MySQL :string is mapped to VARCHAR(255) https://edgeguides.rubyonrails.org/active_record_migrations.html
with MySQL :string is mapped to VARCHAR(255)
:string | VARCHAR | :limit => 1 to 255 (default = 255) :text | TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)
Reference:
https://hub.packtpub.com/working-rails-activerecord-migrations-models-scaffolding-and-database-completion/
When should each be used?
As a general rule of thumb, use :string for short text input (username, email, password, titles, etc.) and use :text for longer expected input such as descriptions, comment content, etc.
:string
:text
2.1m questions
2.1m answers
60 comments
57.0k users