Searching Capabilites

Hi Experts,

I've Model Called Categories. Id and name column
and another model called contacts which has id, name, address, phone,
city, location, neighbourhood and lanmark.

i've implemeneted ferret. (it isnt a problem)

in model for categories and contacts
acts_as_ferret :remote => true

my question is

if i pass a value like this on controller

value = "%jewellary%"
@categories = Category.find_by_contents(value)
@contacts = Contact.find_by_contents(value)

I can get datas from categories. which i've already.

but if i pass value as
value = "%je%"
@categories = Category.find_by_contents(value)
@contacts = Contact.find_by_contents(value)

i dont get any datas displayed.

as far as i understood that ferret searches for whole string like
jewellary.

is there a way to by pass the search?

Thanks in Advance.

regards,
Bala

yes...you can do Contact.find_by_sql(['SELECT * FROM contacts WHERE
contents LIKE ?', '%'+je+'%'])

I think I may be a little out on the syntax...you can check it here:
http://api.rubyonrails.org/ look for "find_by_sql" under "Methods"

Phil Tayo wrote:

yes...you can do Contact.find_by_sql(['SELECT * FROM contacts WHERE
contents LIKE ?', '%'+je+'%'])

I think I may be a little out on the syntax...you can check it here:
http://api.rubyonrails.org/ look for "find_by_sql" under "Methods"

Actually I think those single quotes should be double quotes, not sure
if it makes a difference so that should be :
Contact.find_by_sql(["SELECT * FROM contacts WHERE
contents LIKE ?", "%"+je+"%"])

Also if you want to search for multiple search terms you use question
marks in the SQL and declare all the variables at the end like this:

...["SELECT * FROM tablname WHERE column 1 = ? AND column2 LIKE ? AND
column3 = ?", @variable1, "%"+@variable2+"%", @variable3]

Hi Phil,

Thanks for the reply,

but if i could use find_by_sql then whats the use of Ferret?

Bala

ermmm good point! I wish I'd realised that earlier, might have saved me
a bit of time...

Bala, depending on how big your ferret index is, destroy it and then recreate it as it might be a problem in that. I ran into a similar problem about a month ago and I seem to recall that’s how I fixed it.

Hi Ryan,

Thanks for the reply, you meant to say destroy the ferret plugin or
the index?

regards,
Bala

Bala wrote:

Hi Ryan,

Thanks for the reply, you meant to say destroy the ferret plugin or
the index?

regards,
Bala

the index

Thanks Phil

Phil and Ryan,

Thanks for the reply.

i got the answer just i replaced "%" to "*"

value = value.gsub("%", "*")
categories = Category.find_by_contents(value)

its working perfectly.

Thanks for the effort Guys. :slight_smile:

regards,
Bala