Empty Array is NULL in Rails where find
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(',')
精彩评论