Rails activerecord database relationship

Source: Internet
Author: User

In the demo database, there are two tables: suppliers and accounts. A supplier has an account, and an account belongs to a supplier, which has a relationship.


1. Create a data table

 

2. Modify model and add Link

In this 1:1 relationship, accounts has the foreign key supplier_id. Therefore, you need to add the belongs_to relationship to account. RB, and add the has_one relationship to supplier. RB. Note the rails conventions. When using the rails g command to create a model, use the singular form (the first letter is case-insensitive). The resulting database table name is in the plural form, the Class Name of the model is in the singular form of camper, and the model file name is in the singular form of lowercase.



There are several common options for the relationship:

: Dependent: for the supplier of the has_one relationship, you can add: the dependent option is destroy, delete, nullify, destroy indicates that deleting a supplier will delete its account (including memory objects and database records) at the same time. Delete indicates that deleting a supplier will delete the database records with an account but will not call destroy to destroy the memory objects, nullify indicates that deleting a supplier will terminate the relationship with the account, that is, only setting the foreign key in the account to null.

: Inverse_of: This option appears in pairs to ensure data synchronization in a pair of relationships and avoid the following situations:


Therefore, modify the model again:



3. Link Operations

After a relationship is established, the following methods are automatically added to both ends of the relationship to create the relationship:


That is, it can be used in rails as follows:


Note that for the association = () method, the work at both ends of the relationship is different.


This problem also exists for the 1: n relationship. The relationship established on one party is automatically saved, and the relationship established on multiple parties is not automatically saved. When the Save method should be used, and when it is not required, there is a well-remembered rule. If the model contains a foreign key, then Association = () is called on the model () the created relationship must be saved (the account in the preceding example). If the model does not contain a foreign key, it is not required (the supplier in the preceding example ).

Unlink:


In this example, the relationship between the two methods can be removed and the account object will be deleted.

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.