2

In my Rails 3 app, I am creating a Book model which contains an ISBN column. This ISBN value 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

4 Answers 4

4

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.)

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

2 Comments

not convinced by that reasoning. one can use a 64 bit integer too (10^19). books are not going to grow so fast as ISBN itself has a 10^13 search space.
True. If I was answering this today I would recommend a BIGINT (rails :integer, limit: 8 to force a BIGINT.)
2

ISBN numbers also contain characters i.e hyphen(-) which obviously you cannot store in integer form. So I would recommend you to use string.

Comments

0

ISBN things like thisISBN 978-7-111-27687-6,so obviously string is a good choice

Comments

0

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.

1 Comment

I am not sure if that should be a thumb rule. if the search space is going to be small enough and not going to exceed the max number limits in future, integer makes a very good datatype as its fast on comparison.

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.