0
を通じて私は3つのモデルユニークなリストにhas_many関係
class User < ActiveRecord::Base
has_many :teams, :through => :team_memberships
has_many :team_memberships
end
class Teams < ActiveRecord::Base
has_many :users, :through => :team_memberships
has_many :team_memberships
has_many :clubs, :through => :club_memberships
has_many :club_memberships
end
class Clubs < ActiveRecord::Base
has_many :teams, :through => :club_memberships
has_many :club_memberships
end
を持っている私は、ユーザーがメンバーであるクラブのユニークなリストを取得できるようにしたいです。次の場合:
@teams = User.last.teams
これらのチームが所属するクラブのリストを取得するにはどうすればよいですか。重複がある場合は、リストに1回だけ表示したいと思います。私がしなければ
現在:
<% @user.teams.each do |t| %>
<% t.clubs.each do |c| %>
<%= link_to c.name, c %>
<% end %>
<% end %>
は、私は明らかに完全なリストを取得しますが、私は重複を削除したいです。誰でも修正を提供できますか?
class User < ActiveRecord::Base
has_many :clubs, -> { uniq }, :through => :teams
end
[参照:
<% @user.clubs.each do |c| %>
<%= c.name %>
<% end %>
より良い方法がある場合は私に知らせてください