Ruby model learning-active record associations

Source: Internet
Author: User

I. How to define Association

There are often associations between two models. To solve the complex operation problems caused by these associations, you can define their associations when defining a model. For example, the MERs and orders entities are defined as follows:

class Customer < ActiveRecord::Base   has_many:orders, dependent::destroyend  class Order < ActiveRecord::Base   belongs_to:customerend
Ii. Associated types
  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many
1. belongs_to (corresponding to has_many)
2. has_one
3. has_to (corresponding to belongs_to)
4. has_through: Through refers to the association between objects-to-objects. defined entities have zero or multiple associations with another entity through third-party entities (the relationship between third-party entities and the two is belongs_to ). For example, in the case of a doctor appointment for a patient, each appointment corresponds to a doctor and a patient, but there is a many-to-many relationship between the doctor appointment and the patient.
5. has_one: Through refers to one-to-one association. Similar to passing dependencies, a defined object is associated one-to-one with another entity through a third-party entity. For example, each suppliers has an accounts, and each accounts corresponds to an account_histories.
6. The has_and_belongs_to_many Association is a sort-to-pair association that does not require third-party intervention. For example, components and parts. Each component consists of multiple parts, and each part belongs to multiple components.

Iii. Associated options
  • :as
  • :autosave
  • :class_name
  • :dependent
  • :foreign_key
  • :inverse_of
  • :primary_key
  • :source
  • :source_type
  • :through
  • :validate
1. As: Indicates multi-state association. 2. autosave: if it is set to true, the owner object will automatically save or delete the corresponding operations of its associated entities when performing an operation. 3. class_name: the associated object name cannot find the corresponding object. You can set the actual object through this attribute. 4. dependent: when the owner object is destroyed, the behavior of the associated object: Destroy: all associated entities are destroyed delete_all: all associated entities are directly deleted from the database and cannot be recovered nullify: if the foreign key is set to null, restrict_with_exception cannot be restored. if an exception is thrown, the system prompts restrict_with_error. If an error is thrown, the system prompts 5. foreign_key: column name of the foreign key defined. 6. inverse_of: specify the reverse Association object 7. primary_key: Specify the ID of the Association Item 8. Through: Specify the third-party entities used by the many-to-many relationship 9. Validate: If set to false, the link will be invalid.


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