pepa007 wrote:
Hi,
I'm trying to find all rows in database where 'title' is a substring
of my specified string. That means if my string is "whatever", then I
want all rows with title being "whateve", "whatev", "whate", "what".
etc.
Here's the function:
def self.superiors(currentitem)
@array =
s = currentitem.title
charcount = (s.index('-')-1)..(s.length-1)
for i in charcount
this = s[0..i]
found = General.find(:all, :conditions => ["title = ?", this])
@array << found
end
return @array
end
if I return 'found' it works, but of course returns the last value
found. but returning @array doesn't. it seems that I'm just doing
something wrong with the arrays?
Thanks for any help.
Hm, if I understand correctly, you are wanting to do the reversal of a
standard database LIKE search. So, in normal conditions, you'd be doing
something like
select * from some_table where some_field like '%some_string%'
I just did a quick test on PostgreSQL, and you can do something like
select * from some_table where 'some_string' like '%' || some_field ||
'%"
I have a table that lists sports (baseball, football, soccer, etc) and I
did the following query
select * from available_sports where 'SoccerMom' ilike '%' || name ||
'%'
and it returned 'Soccer' as I expected it to.
Note that || is the string concatenation operator in Postgres.
Depending on your database server, you might also consider using a
case-insensitive operator, which is ilike in Postgres.
Doing it in the database, you will only have one query. Using the method
you are trying now, you will have a query for every variation of the
search string.
Peace,
Phillip