Should I use a Rails integer or string for this number?
In my Rails 3 app, I am creating a Book
model which contains an ISBN
column. This ISBN
valu开发者_如何学运维e will be a number 13 characters long. Which type should I use to create the ISBN
column? Should I use an :integer
or a :string
? What is the maximum size of an integer in Rails?
rails g scaffold Book title:string isbn:integer
The maximum size of the integer depends on the database being used. For MySQL it's 2147483648 (2^31 since it's an unsigned integer.) I would recommend you store the ISBN as a string (likely a CHAR(13) if possible.)
ISBN numbers also contain characters i.e hyphen(-) which obviously you cannot store in integer form. So I would recommend you to use string.
ISBN things like thisISBN 978-7-111-27687-6
,so obviously string is a good choice
I think a good rule of thumb is using numerical fields when you need to do math operations on the fields (or when there is a chance you would do it at some point). If not, it is a very good candidate of being an string.
精彩评论