Each answer on this page is incorrect because none of these answers care about ALL array cases, especially arrays that have only one element .
Here is an example that will be FAIL using any of the so-called solutions on this page:
@ids = [1] Post.where("publisher_id NOT IN (?)", @ids)
The only answer on this page that is actually on the right track and will take care of this very important case is @Tudor Constantin's. But the problem is that he did not actually show the “way” to use his methodology to solve the real abstract sample question OP (not only using hard-coded numbers).
here is my solution for dynamically searching for identifiers not included in the Activerecord association given by an array of identifiers for exclusion that will work with an array of n elements (... including n = 1 and n = 0)
@ids = [166] @attribute = "publisher_id" @predicate = "NOT IN" @ids = "(" + @ids.join(",") + ")" if @ids == "()"
If this helped you, please vote! If you are embarrassed or do not understand one of my comments, let me know.
source share