微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

查找在值数组中找到的所有:condition id

如何解决查找在值数组中找到的所有:condition id

| 我有2个模型(通过模型
lnkteamplayer
链接
player
team
Team has_many players through lnkteamplayer
Player has_many teams through lnkteamplayer
我需要检索不属于特定团队的所有球员。
<% @players = Player.find(:all,:conditions => [\"id != ?\",@team.lnkteamplayers.player_id ]) %>
以上代码行出现错误。我的问题是在上述条件下如何传递值数组。 感谢您提供的任何建议。     

解决方法

        您在那里遇到了一些问题: 1)条件的第一部分\“ id!=?\”是sql的一个片段,在sql中,您将\“ not equals \”表示为
<>
而不是
!=
。例如
\"id <> ?\"
2)要使用数组,sql语法为
id in (1,2,3)
id not in (1,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])
    ,        由于您未提供错误消息,因此我在这里有点猜测。但是,我认为ѭ6dial在许多SQL方言中不是有效的语法。您可能正在寻找类似
NOT IN ()
的东西。 同样,
@team.lnkteamplayers.player_id
可能不起作用,因为从
@team.lnkteamplayers
返回的值可能没有
player_id
方法。您可能需要实际玩家的ID。 可以使用ѭ17之类的东西来完成。 总而言之,您的行可能需要看起来像
<% @players = Player.find(:all,:conditions => [\"id NOT IN (?)\",@team.lnkteamplayer_ids]) %>
但是如果没有更多信息,我们将无法确定。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。