exists?
AR::Base.exists? used to basically say !find(:first, :conditions =>)...
It now (effectively) says !find(:first, :select => 'id', :conditions => )
I imagine this is to save on instantiating a large object with many columns just to throw it away. This is all good and well, but has broken some things for us: on some models we have after_find callbacks that rely on the presence of attributes. In our particular cases we just moved to setting up those things lazily, you could also check that the attributes are actually here. However morally I wouldn't expect exists? to instantiate anything at all, which would make the problem go away (and in a sense take the above change a little further).
I'm the author of the new exists? code, and I also looked at doing it with a lower-level connection call instead of a find with :select option. I don't have the data around anymore, but as I recall, the benchmarks showed that using the select_all approach didn't perform as well, so I went with the find with :select approach. I hadn't thought about the after_find callback interaction, and I guess no one else has run into that problem before now either. Some stuff has changed in AR that might have affected performance of the select_all approach since I tried it last, so it might be worth revisiting now.
I'm the author of the new exists? code, and I also looked at doing it
with a lower-level connection call instead of a find with :select
option. I don't have the data around anymore, but as I recall, the
benchmarks showed that using the select_all approach didn't perform as
well, so I went with the find with :select approach. I hadn't thought
about the after_find callback interaction, and I guess no one else has
run into that problem before now either. Some stuff has changed in AR
that might have affected performance of the select_all approach since
I tried it last, so it might be worth revisiting now.
Well I ended up with that code by taking apart find :first and
removing the instantiation of rows at the end, so I don't think it
should be a problem.
What I posted should be doing exactly what find :first does, but
without the instantiation
Well I ended up with that code by taking apart find :first and
removing the instantiation of rows at the end, so I don't think it
should be a problem.
What I posted should be doing exactly what find :first does, but
without the instantiation
A count makes the database do a lot more work as it must count all
matching rows not just find the first one that matches. Depending
on the exact conditions used and the database schema/indices/size the
difference can be very large.