Ruby on Rails入門 レッスン7
Active Record詳解
アソシエーション(has_many/belongs_to)、スコープ、コールバックを学びます。
アソシエーション
Active Recordのアソシエーションはモデル間の関連を宣言的に定義します。
has_many :comments— 1対多の関連(親側)belongs_to :post— 1対多の関連(子側)has_many :through— 多対多の関連scope :published, -> { where(published: true) }— スコープ
例1: has_many / belongs_toアソシエーション
Rubyエディタ
例2: スコープとコールバック
Rubyエディタ