nils returning in results collection

4 views
Skip to first unread message

Zach

unread,
Jul 14, 2008, 3:04:33 PM7/14/08
to Thinking Sphinx
## Foo Controller
@results = Foo.by_location(city, state).search(@term)

## Foo model
named_scope :by_location, lambda { |city, state|
{ :select => 'foo.*',
:conditions => [ "foo_addresses.city = ?
AND
foo_addresses.state = ?", city,
state], :joins =>
"LEFT JOIN `foo_addresses` ON
foo_addresses.foo_id = foo.id", :order => "name" }}

Pat Allan

unread,
Jul 14, 2008, 4:44:53 PM7/14/08
to thinkin...@googlegroups.com
Hi Zach

named_scope is purely for SQL-based queries, not Sphinx queries - they
work in different ways.

People have requested named_scope support, but I'm thinking that might
not be possible - although a similar function just for Sphinx queries
might work (ie: named_sphinx_scope, or something along those lines).
Either way, nothing's implemented yet.

Cheers

--
Pat

Zach

unread,
Jul 15, 2008, 10:58:58 AM7/15/08
to Thinking Sphinx
Thanks for getting back to me. What is the best workaround for this?
Is there a way of adding custom SQL to the TS query?

Pat Allan

unread,
Jul 15, 2008, 5:49:04 PM7/15/08
to thinkin...@googlegroups.com
SQL itself is no use - but your options are either to add city and
state to your fields (if they're not already) and do the following:

Foo.search @term, :conditions => {:state => state, :city => city}

OR, push them out into their own tables, use foreign keys, add those
keys to your attributes, and then use something like:

Foo.search @term, :with => {:state_id => state.id, :city_id => city.id}

I'm not sure, but the latter might be faster from a Sphinx perspective.

Cheers

--
Pat

Zach

unread,
Jul 24, 2008, 1:46:36 PM7/24/08
to Thinking Sphinx
Thanks Pat. As always your help is appreciated.

The issue I am having is that I am trying to search through a model.

Foo.address.city, Foo.address.state. Address is has_one but never used
by more than 1 Foo.

Could you advise me any ways in doing this?

Nested conditions did not work as I suspected.

Zach

unread,
Jul 24, 2008, 4:23:25 PM7/24/08
to Thinking Sphinx
I've fixed most of this. My only issue now is that I can't pass in an
array to a condition like I can with ActiveRecord.find.

I want to do: :state => %w(one two)

any way to do this with whats given in the current ThinkingSphinx?

What do you

Pat Allan

unread,
Jul 24, 2008, 5:06:20 PM7/24/08
to thinkin...@googlegroups.com
Hi Zach

Field conditions get translated to text - as that's what Sphinx
requires.
So, you could try the following:

:state => "(one | two)"

Or, if you've already got an array:

:state => "(#{states.join(' | ')})"

For general extended query syntax, see
http://sphinxsearch.com/doc.html#extended-syntax
(Extended query syntax is the default *if* you're searching on certain
fields).

Cheers

--
Pat

Reply all
Reply to author
Forward
0 new messages