I was working with named_scope earlier today and I was have an issue getting it to work properly with a Thinking Sphinx search that I had. This makes sense, as named_scope is not completely supported yet by Thinking Sphinx. Read below for the gotcha and a short-term work around.
I had a named_scope something like this:
I was calling it in a chain to another method which just called a Thinking Sphinx search:
My problem came from what came back in my @posts:
All looks fine. But then I would hit a nil object error… huh?
Loading up a script/console, I was finally able to see the problem.
Somehow combining named_scope with Thinking Sphinx decided to load three posts into my @posts object even though it only found one. So I had something like this:
And here was my fix (after the query):
This throws out the nil results. Hopefully this will help out those out in Google land.
Questions? E-mail me: this domain AT gmail DOT com