find all:条件id在值数组中找到

tan*_*nya 1 ruby-on-rails

我有2个模型(playerteam通过模型链接lnkteamplayer)

Team has_many players through lnkteamplayer
Player has_many teams through lnkteamplayer
Run Code Online (Sandbox Code Playgroud)

我需要检索所有不属于特定团队的玩家.

<% @players = Player.find(:all, :conditions => ["id != ?",@team.lnkteamplayers.player_id ]) %>
Run Code Online (Sandbox Code Playgroud)

上面的代码行我收到错误.我的问题是如何在上述条件下传递一组值.

感谢您提供的任何建议.

Max*_*ams 6

你有几个问题:

1)条件的第一部分,"ID!=?",是SQL的片段,并在SQL这样做"不等于"作为<>没有!=.例如"id <> ?"

2)要使用数组,sql语法是id in (1,2,3)id not in (1,2,3).在你的条件下,你可以这样做:conditions => ["id not in (?)", array_of_ids]

所以,你可以让球员不在这样的球队:

@team = Team.find(params[:team_id])
@not_on_team = Player.find(:all, :conditions => ["id not in (?)", @team.player_ids])
Run Code Online (Sandbox Code Playgroud)