1
0

More than 3 years have passed since last update.

has_manyで、scopeを使って絞り込む

Posted at

merge で scope を指定すればOK。

user.rb
has_many :team_members
has_many(
  :pro_team_member,
  -> { merge(Team.pro) },
  through: :team_members,
  source: :team,
)

「何をもってproとするか」は、 Team 側が持つべき知識なので、 User 側で無闇に where(pro: true) みたいなコードを書くのは避けた方がよいです。

ドメイン知識が他のオブジェクトに流出している感じがするので。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0