开发者

Empty Array is NULL in Rails where find

开发者 https://www.devze.com 2023-03-22 01:18 出处:网络
I have the following simple where find condition: Player.where(开发者_高级运维\'id NOT IN (?)\', @groups.collect {|g| g.player_ids}.flatten)

I have the following simple where find condition:

Player.where(开发者_高级运维'id NOT IN (?)', @groups.collect {|g| g.player_ids}.flatten)

So this finds all players that are currently not in any groups, right? Well, the problem is that if there are no players in any groups, nothing is shown. The reason for this is the following SQL is generated:

SELECT "players".* FROM "players" WHERE (id NOT IN (NULL))

This seems like a strange default to me, and I've tested it in Rails 3.0.7 and 3.1.0.rc4 with the same result. Now I could create some conditions if @groups.collect {|g| g.player_ids} is empty, but is there a better way to do this?


You may not like this any better, but...

Player.where('id not in (select id from players where id in (?))', 
    @groups.collect {|g| g.player_ids}.flatten)


@groups.collect {|g| g.player_ids}.flatten.join(',')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号