I want to do something like this:
puts Room.find(1,2,3) - Room.find(1,2)
so that it does Room.find(3)
and returns that SQL record only instead of the 3 records that Room.find(1,2,3)
gives me
I think this will be much more efficient:
first_id_list = [1, 2, 3]
second_id_list = [1, 2]
Room.find(first_id_list - second_id_list)
That way, you only search for the id's that you really want to be in your query, instead of fetching unwanted rows and then instantiating a bunch of Room objects that you then promptly ignore but Ruby still has to garbage collect.
As Anthony Alberto pointed out in the comment. This works. Only case this won't work is when you find just one instance of room. For example Room.find(1)
will return an object of class Room
, rather than an Array. Therefore you won't be able to use the binary operator -
on this Speak object. To avoid this from happening, use to_a
method.
Array.wrap(Room.find(1,2,3)) - Array.wrap(Room.find(1))
=> Room.find(2,3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With