这是我的Teams-Controller.rb
@my_teams = current_user.teams.all
@available_teams = Team.where.not(@my_teams.all)
end
我正在尝试显示该用户当前尚未加入的所有团队。我有三个模型Team,TeamMember和User。目的是使用户进入可用团队的显示方法,并允许他们加入该团队。 团队模型team.rb
class Team < ApplicationRecord
has_many :team_members
has_many :users, through: :team_members
end
用户模型user.rb
class User < ApplicationRecord
has_many :team_members
has_many :teams, through: :team_members
end
团队成员模型
class TeamMember < ApplicationRecord
belongs_to :user
belongs_to :team
end
答案 0 :(得分:0)
尝试这种方式
@my_teams = current_user.teams
@available_teams = Team.where.not(id: @my_teams.pluck(:id))
基本上我们想要实现的是这样的SQL查询
SELECT *
FROM teams
WHERE id NOT IN (1, 2, 3)