I don't think current_user.tags.pluck(:id)
is returning you a nil
, it is returning an empty array. ActiveRecord will treat an empty array as a NULL in that context. The result is some nonsensical SQL like this:
select tags.* from tags where id in (null)
Due to the idiosyncrasies of SQL's NULL (in particular x = NULL
and x != NULL
are both false for all x
), an in (null)
or not in (null)
in a WHERE clause won't match anything.
Rails converting Ruby's []
to NULL
is pretty stupid (more discussion on that over here) but even if it was smart enough to raise an exception, you'd still have to deal with the "empty array" case manually with something like this:
tag_ids = current_user.tags.pluck(:id)
if(tag_ids.empty?)
tags = Tag.all
else
tags = Tag.where('id not in (?)', tag_ids)
end
And you don't need the uniq
in there, the SQL in
operator will treat its RHS as a set so duplicates will be collapsed behind the scenes.