Ruby on Rails development from scratch (42)-ActiveRecord base (primary key and ID)

Source: Internet
Author: User
Tags integer ruby on rails

As you may have noticed, in our previous code, the database definition uses an integer field ID as the primary key, which is a convention for the active record.

Perhaps you should ask, why not use the order number or a meaningful column as the primary key? An important reason for using IDs as primary keys is that if you use a primary key with an intrinsic format, it is possible that the rules will change over time. For example, an ISBN number is used to make a key to the Book table, after all, the ISBN number is unique, but it is possible that when one is finished, the publishing industry in the United States has developed and appended a digit to all the ISBN numbers.

If we use ISBN as the primary key for the Book table, we're going to update all the Book table records to reflect this change, and there's another problem, and there are other tables that reference the primary key to the Book table, and we're going to update all the references, which also involves removing the foreign keys, all of which are very painful.

If you use a meaningful value as the primary key, then we will receive the influence of the outside business rules, if we use IDs, we can control ourselves completely, and if something like ISBN changes, it will not affect the database structure.

If you start with a new database structure, you might follow the Convention and use an ID as the primary key for all tables, but when you're using a surviving database, the Active record provides a simple way for you to assign a primary key to the table again, for example:

Class Badbook < activerecord::base
set_primary_key "ISBN"
end

Typically, an Active record takes note of generating primary key values for newly created records-using a self growing integer. However, when you override the primary key name of the table, you need to be responsible for creating a unique primary key value for the new record. Perhaps a little surprising, you still set an id attribute to do this, because the active record is concerned that the primary key setting always uses the name ID attribute, and the Set_primary_key declaration simply sets the name of the column used, the following example, We use ISBN as the primary key.

Book = badbook.new
book.id = "0-12345-6789"
book.title = "My Great American Novel"
book.save
# ...
Book = Badbook.find ("0-12345-6789")
puts Book.title # => ' My Great American Novel '
p book.attributes #=> {' I SBN "=>" 0-12345-6789 ",
" title "=>" My Great American Novel "}

That is, when you set a primary key, use the id attribute, and at other times, use the real column name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.