Ruby/rails/db関連付け
提供: 初心者エンジニアの簡易メモ
関連付け一覧
belongs_to has_one has_many has_many :through has_one :through has_and_belongs_to_many
belongs_to関連付けオプション
:autosave :class_name :counter_cache :dependent :foreign_key :primary_key :inverse_of :polymorphic :touch :validate :optional
1:n関連のデータ取得例
class Battle < ApplicationRecord belongs_to :user end class User < ApplicationRecord has_many :battle end
battles = Battle.where("user_id = ?", params[:id]) battles.each do |battle| logger.debug("battle.user.name=#{battle.user.name}") end