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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
u can try like this
found = General.find(:all, :conditions => ["title
=?",''%#{this}%''])
or
 General.find(:all, :conditions =>
["title=?","''%''"+this+"''%''"])
:)
On Sat, Jun 21, 2008 at 2:31 PM, pepa007
<richter.josef-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
oops a small modification General.find(:all, :conditions => ["title=?","''%"+this+"%''"]) On Sat, Jun 21, 2008 at 3:23 PM, bala kishore pulicherla < balumca21-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> u can try like this > > found = General.find(:all, :conditions => ["title =?",''%#{this}%'']) > or > General.find(:all, :conditions => ["title=?","''%''"+this+"''%''"]) > > :) > > On Sat, Jun 21, 2008 at 2:31 PM, pepa007 <richter.josef-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. >> >> >> >> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bala''s suggestion is correct if you''d do the opposite of your search. That is having "ever" and finding "whatever", "forever" and so on. For your kind of search I dont see an easier way right now (but I am sure there is, in ruby there''s always an easier way for these things..:) Nevertheless you should try to merge the arrays with "@array += found", that will add entries to the existing @array "@array << found" will create an array of array''s since General.find returns an array, I''m not sure if that''s what you want.... good luck! smn On Jun 21, 11:01 am, pepa007 <richter.jo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bala''s suggestion is correct if you''d do the opposite of your search. That is having "ever" and finding "forever", "whatever" and so on. For your search I dont see an easier way, though I think there is one, as always with ruby ..:) You shoul try to fill your array like this though: @array += found, this will add the entries of found to your existing @array. @array << found, will create an array of arrays, I am not sure if that''s what you want.... good luck! smn On Jun 21, 11:59 am, "bala kishore pulicherla" <balumc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> oops a small modification > > General.find(:all, :conditions => ["title=?","''%"+this+"%''"]) > On Sat, Jun 21, 2008 at 3:23 PM, bala kishore pulicherla < > > balumc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > u can try like this > > > found = General.find(:all, :conditions => ["title =?",''%#{this}%'']) > > or > > General.find(:all, :conditions => ["title=?","''%''"+this+"''%''"]) > > > :) > > > On Sat, Jun 21, 2008 at 2:31 PM, pepa007 <richter.jo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
wow, pretty close, thanks! I didn''t know I can switch parameters in
LIKE clause this way...
BUT! I''m using MySql not Postgre and || operator means OR in MySql so
the result of your query is very different in MySql :-) Using
CONCAT(title,''%'') didn''t work but don''t know
why. just returned no
results.
HOWEVER, it helped me find the solution which is REGEXP function
so my query looks like SELECT * FROM ''generals'' WHERE
''^title'' REGEXP
''whatever''
and the RoR function looks like
  def self.superiors(currentitem)
    @found = General.find_by_sql("SELECT * FROM `generals` WHERE
''^"+currentitem.title+"'' REGEXP title")
  end
it seems to do exactly what I want, but it''s much shorter...
On Jun 21, 1:51 pm, Phillip Koebbe
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> 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
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---