views:

26

answers:

1

Contact belongs_to status_contacts

I only want those Contacts where no value has been assigned.

I installed the searchlogic plugin.

I tried:

contacts = Contact.status_contact_null

And got an error.

How can I get a full sense of how to use associations with searchlogic, and how can I use it for this particular search?

A: 
Contact.status_contact_id_is(nil)

should generate SQL that looks like this:

SELECT * FROM `contacts` WHERE (contacts.status_contact_id IS NULL)

Searchlogic substitutes Ruby's nil for SQL's null.

You can do the reverse with ne (not equal)

Contact.status_contact_id_ne(nil)

There are a bunch of good examples on the github page for Searchlogic

zetetic
oh, I thought when I checked out hte github page that it understood associations? It had an example of Users and Orders...how is this different (versus needing to call-out the foreign key?)
Angela
It does understand associations, which you can see by chaining methods in the scope -- eg. if `status_contacts` has a `name` column, `Contact.status_contact_name_is("Fred")` will work. But when the method exists on the model itself (eg. `status_contact_id`) it uses that instead, which is the only way to make this query work, really. You could write your own named scope to make this more readable, if you liked
zetetic
I see...but isn't status_contact an association? Even with that association, it still needs the foreign-key? Am having difficulty figuring out how to use search-logic -- is that the only exception? Wouldn't all associations, by definition, have a foreign-key?
Angela
True, `status_contact` is an association, but the association cannot be used in this particular query because when the foreign key is nil there is (by definition) no associated record to link to. You can use the association for other methods (like the `name` example I gave), when the foreign key is not `null`.
zetetic
I see...so it is because it is nil...but what about the method they have _not_null? and _null? in the examples...? Isn't that checking for null?
Angela
Yes, it looks like `model_null` is equivalent to `model_id_is(nil)` -- I missed that :)
zetetic
Note: a handy way to explore the underlying SQL is to add a method to log queries to the console as explained here (see `loud_logger`): http://www.fromjavatoruby.com/2008/10/irbconsole-tweaks.html
zetetic