โ Ruby on Rails learning pathRuby on Rails ยท Module 6 of 10
Associations and validations
Models link to each other and protect data quality.
๐ Key ideas
- belongs_to :category and has_many :products link tables via category_id.
- has_many :through models many-to-many relations.
- validates :name, presence: true blocks blank names.
- validates :email, uniqueness: true checks for duplicates.
- errors.full_messages lists why a save failed.
๐ก Real-life example: Like a school: each student belongs_to a class, and each class has_many students.
belongs_toModel holds the foreign key
has_manyOne record linked to many
validatesDeclares data rules
has_many :throughMany-to-many via a join model