"Ruby" Ruby's model learning--active Record Associations

Source: Internet
Author: User
Tags throw exception

In the process of reading no matter what the problem, welcome to communicate together

Email:[email protected]

qq:1494713801

First, how to define the association

There are often associations between the two model, and in order to solve the complex operational problems caused by these associations, it is possible to define their association relationships when the model is defined. such as: Entity customers and orders definitions such as the following:

classCustomer < ActiveRecord::Base   has_many:orders, dependent::destroyend  classOrder < ActiveRecord::Base   belongs_to:customerend
Ii. Types of associations
    • belongs_to
    • has_one
    • has_many
    • has_many :through
    • has_one :through
    • has_and_belongs_to_many
1, belongs_to (corresponding with Has_many)

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmduxntc2mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "width=" 368 "height=" 145 ">
2, Has_one
3, Has_many (corresponding with belongs_to)

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmduxntc2mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "width=" 335 "height=" 145 ">
4. Has_many:through refers to the Many-to-many Association, the defined entity has 0 or more associations with another entity through a third-party entity (the relationship between the third-party entity and both is belongs_to).

such as: sick I to make an appointment to the Doctor's sample, each appointment is corresponding to a doctor and a patient. But there is a lot of relationship between doctors and patients by appointment.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmduxntc2mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "width=" 324 "height=" 148 ">
5. Has_one:through refers to the one-to-one association, which is similar to transitive dependency, and the entity defined is a one to another entity through a third-party entity. For example: Each suppliers has a accounts, and each accounts corresponds to a account_histories.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmduxntc2mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "width=" 339 "height=" 142 ">
6. The Has_and_belongs_to_many Association means a many-to-many connection that does not require third party intervention. For example: Components and parts, each component contains multiple parts, and each part belongs to more than one component.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmduxntc2mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "width=" 332 "height=" 101 ">

Third, the associated options

    • : As
    • : AutoSave
    • : class_name
    • : Dependent
    • : Foreign_key
    • : inverse_of
    • :p Rimary_key
    • : source
    • : Source_type
    • : through
    • Code>:validate
1, as: Indicates Polymorphic Association 2, AutoSave: If set to true. When an owner entity makes an action, it voluntarily saves or deletes the corresponding action for its associated entity. 3, Class_name: The associated entity name cannot find the corresponding entity, and the actual entity is set through this property. 4. Dependent: When the owner entity is destroyed, the behavior of the associated entity: Destroy: All associated entities are destroyed Delete_all: All associated entities are deleted directly from the database, unrecoverable nullify: foreign keys are Set to NULL, unrecoverable restrict_with_exception: Throw exception hint Restrict_with_error: Throw error prompt 5, Foreign_key: Define column name for foreign Key 6, inverse_of : Indicates the inverse associated entity 7, Primary_key: Indicates the associated item ID 8, through: Indicates that a many-to-many relationship uses a third-party entity 9, validate: If set to false, the association relationship will be invalid.




"Ruby" Ruby's model learning--active Record Associations

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.