1. The importance of performance optimization
Reduce server stress (quantization is the execution time)
Increase the user's goodwill (quick!) )
2. ActiveRecord: Table Association
ActiveRecord can use associations to define the correlation between tables. So far we have learned to use ActiveRecord to manipulate the database, but have not yet fully developed the feature of the relational database, which is to correlate tables through primary key and foreign keys
Primary key This field is in rails, which is conventionally called an ID, and the type is an integer and incremented. The foreign key field is known as the {model_name}_id, and the type is an integer
3. Relationship Associations
Belongs_to,has_one,has_many parameter class_name, Foreign_key,conditions,order, dependent
Dependent can be set to delete its Has_many object when the object is deleted:d Estroy will perform attendee destroy callback:d Elete will not perform attendee destroy callback: Nullify this is the default value, Won't help. Delete Attendee
Many-to-many, trough, such as has_many:firms,: through =: firm_brands
4. Talk about include
The event object captured through joins does not include its associated object. If a record of its associated object is required, includes is used.
Includes can pre-read the associated records to avoid n+1 problems
No relationship, no include, so use Belongs_to,has_one,has_mang more.
5. Habits
Do not use double quotation marks when you can use single quotation marks.
Try to be as simple as find (: All)
Unnecessary return, unnecessary assignment
Methods (especially instance methods) to minimize the query, multi-use relationships, in the include can be very good to avoid n+1
Rails performance Optimization