users
|id|name|
|1|たかし|
|2|さとる|
|3|ももか|
categories
|id|cate_name|
|1|Aクラス|
|2|Bクラス|
|3|Cクラス|
|4|Dクラス|
categories_users
|user_id|category_id|
|1|2|
|1|3|
|2|4|
|2|3|
class Category < ApplicationRecord has_many :category_users has_many :users, through: :category_users end
class User < ApplicationRecord has_many :category_users has_many :categories, through: :category_users end
class CategoryUser < ApplicationRecord belongs_to :category belongs_to :user end
railsで一般的に見られる、has_many throughのモデルケースです。
この場合、cクラスカテゴリ(id 3)を持っているユーザー一覧を取得するにはどのようにsqlを書けばいいのでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。