Details about the association between models in ruby on rails: rubyrails

Source: Internet
Author: User
Tags ruby on rails

Details about the association between models in ruby on rails: rubyrails

Preface:
Before learning model Association, remember the following points:
1. Link. Both ends must be written. Otherwise, errors that beginners cannot understand may occur. It is also very beneficial to understand the code.
2. The model name is singular and the controller is plural.
3. blong_to must be followed by a singular number and be in lowercase. Has_many must be followed by a plural number.

1. One-to-multiple

For example:
Mother Wang has two children, Xiao Ming and Xiao Liang. It can be said that mother Wang has multiple children. It can also be said that James has a mother; Tom has a mother. We generally Design tables in this way:
Id and name in the mothers table
The sons table has IDs and names.
In order to increase the logical relationship, the primary and foreign key relationships will be added to one column on multiple sides. Therefore, the sons table has three columns: id and name and mother_id (corresponding to the mothers table id)
Common SQL:

Select test_associate.mothers.name from test_associate.mothers inner join test_associate.sons on sons. mother_id = mothers. id where sons. name = 'lily'

Ruby code:

class Mother  has_many :sons end class Son  belongs_to :mother end

Explanation: one mother has multiple children, and one son belongs to one mother.
We can test it on the rails console:
Xiao_wang = Son. first
Mom = xiaowang. mother

This. mother method is generated by class Son's belongs_to: mother statement.
That is, it is equivalent to the following SQL statement:

select * from mothers   join sons   on sons.mother_id = mothers.id   where sons.id = 1

Explanation:

A: belongs_to: mother
B: belongs_to: mother,: class => 'Mother ',: foreign_key => 'Mother _ id'
A = B

This is the most typical method of Rails programming based on conventions. It declares which table corresponds to which class, and declares the association between classes.
1. belongs_to: mother, rails can determine that the mothers table is the end of one. The current class is: "class Son", so rails will know the relationship between the two tables.
2.: class => 'Mother ', indicating the end of a. The corresponding model class is Mother. According to the rails convention, the Mother model corresponds to the mothers table in the database.
3.: foreign_key => 'Mother _ id', rails will know, and the foreign key is 'Mother _ id '. in a one-to-multiple relationship, the foreign key is stored at the multiple end (that is, the sons). Therefore, in the sons table, a column must exist, called mother_id)
Therefore, this complex SQL condition is ready and can be generated.
After the preceding ruby code is configured, you can call it as follows:

Son = Son. firstson. mother #. mother method is generated by belongs_to in class Son. The mother = Mother. firstmother. sons #. sons method is generated by hash_many in class Mother.

2. One-to-one, Which is simple and not commonly used. It is not described here. (Husband and Wife)

Iii. many-to-many

For example:
One student has multiple teachers (who have learned multiple courses)
A teacher can teach multiple children (teach a course, but many students come to listen to it)
We often do this:
Students has two fields: id and name.
Teachers has two fields: id and name.
It is not suitable for any table. This is an intermediate table, that is, a bridge table.
Lessons has IDs, names, student_id, and teacher_id.
Original SQL:

Select teachers. *, students. *, lessons. * from lessons from teachers, join teachers on lessons. teacher_id = teachers. id join students on lessons. student_id = students. id where students. name = 'wang'

Ruby code:

class Student  has_many :lessons  has_many :teachers, :through => :lessons end

Tip: has_many: teachers,: through =>: lessons is equivalent
Has_attributes: teachers,: class => 'teacher',: foreign_key => 'Teacher _ id',: throught =>: lessons
Class Teachers
Has_ons: lessons
Has_tables: students,: through =>: lessons
End

Check which of the following are the original SQL statements of Mr. Wang.

Student. find_by_name ('wang '). teachers

The above is all the content shared in this article. The examples are very simple and easy to understand. I hope you will like them.

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.