I''m not using ferret because the search I need is not that complicated. I created a basic active record search. I''m using some code from the pragmaticprogrammer book. This is the issue: I can get the single keyword to work fine when entered into the search box, but when you put to keywords separated by a space the search returns no results. It makes sense since the function below was structured for a single key search. So I tried to modify it by getting the string, splitting it and trying to do a loop for the newly splitted string. Here is when I get the error "can''t convert Array into String". I''m sure that I''m doing some logical mistakes but I''m new to ruby on rails so please forgive me. hope some one can help def conditions_by_like(value, *columns) myArray = value.gsub(/[^\A-Za-z0-9\s]/, "").split(/ /) myArray.each do |itemz| columns = self.user_columns if columns.size==0 columns = columns[0] if columns[0].kind_of?(Array) conditions = columns.map { |c| c = c.name if c.kind_of? ActiveRecord::ConnectionAdapters::Column "`#{c}` LIKE " + ActiveRecord::Base.connection.quote("%#{itemz}%") }.join(" OR ") end end -- 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 -~----------~----~----~----~------~----~------~--~---
On 26 Jun 2008, at 12:55, Sam Ginko wrote:> > I''m not using ferret because the search I need is not that > complicated. > I created a basic active record search. I''m using some code from the > pragmaticprogrammer book. This is the issue: > > I can get the single keyword to work fine when entered into the search > box, but when you put to keywords separated by a space the search > returns no results. It makes sense since the function below was > structured for a single key search. So I tried to modify it by getting > the string, splitting it and trying to do a loop for the newly > splitted > string. Here is when I get the error "can''t convert Array into > String". > I''m sure that I''m doing some logical mistakes but I''m new to ruby on > rails so please forgive me. > > hope some one can help > > def conditions_by_like(value, *columns) > > myArray = value.gsub(/[^\A-Za-z0-9\s]/, "").split(/ /) > myArray.each do |itemz| > columns = self.user_columns if columns.size==0 > columns = columns[0] if columns[0].kind_of?(Array) > conditions = columns.map { |c| > c = c.name if c.kind_of? > ActiveRecord::ConnectionAdapters::Column > "`#{c}` LIKE " + > ActiveRecord::Base.connection.quote("%#{itemz}%") > }.join(" OR ") > end > endYou''re not returning the value you think you are (each always returns the collection you search for). Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 26 Jun 2008, at 12:55, Sam Ginko wrote: > >> the string, splitting it and trying to do a loop for the newly >> myArray = value.gsub(/[^\A-Za-z0-9\s]/, "").split(/ /) >> end > You''re not returning the value you think you are (each always returns > the collection you search for). > > Fredso what would be the correct way? I tested the above loop with a simple string(below) and it works <% theValue = "Paris in the Spring" myArray = theValue.gsub(/[^\A-Za-z0-9\s]/, "").split(/ /) myArray.each do |itemz| %> <%= itemz %><br> <% end %> -- 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 -~----------~----~----~----~------~----~------~--~---
On 26 Jun 2008, at 14:22, Sam Ginko wrote:> > Frederick Cheung wrote: >> On 26 Jun 2008, at 12:55, Sam Ginko wrote: >> >>> the string, splitting it and trying to do a loop for the newly >>> myArray = value.gsub(/[^\A-Za-z0-9\s]/, "").split(/ /) >>> end >> You''re not returning the value you think you are (each always returns >> the collection you search for). >> >> Fred > > > so what would be the correct way? I tested the above loop with a > simple > string(below) and it works > > > <% > theValue = "Paris in the Spring" > myArray = theValue.gsub(/[^\A-Za-z0-9\s]/, "").split(/ /) > myArray.each do |itemz| > %> > <%= itemz %><br> > <% > end > %>that''s completely different (since here you''re not using the return value of each, whereas in your other example you are) my point is that if you do x = [1,2,3].each { ...} then x will always be set to [1,2,3], so in your case your conditions_by_like function always returns myArray and completely discards all the processing you do in the each block. Depending on what you want to do you might want to use map/collect or just accumulate the result in a local varaible and return that. Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
> that''s completely different (since here you''re not using the return > value of each, whereas in your other example you are) > > my point is that if you do > > x = [1,2,3].each { ...} > > then x will always be set to [1,2,3], so in your case your > conditions_by_like function always returns myArray and completely > discards all the processing you do in the each block. > Depending on what you want to do you might want to use map/collect or > just accumulate the result in a local varaible and return that. > > FredI understand the logic now. But could you give me an example on how I would do a map/collect or accumulate it in a variable? I just want to make sure that if I type in 2 words separated by a space i:e John Doe both John and Doe will be processed. I''m not a ruby expert and just started so I would really appreciate your help and Patience. Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
On 26 Jun 2008, at 14:52, Sam Ginko wrote:> > >> that''s completely different (since here you''re not using the return >> value of each, whereas in your other example you are) >> >> my point is that if you do >> >> x = [1,2,3].each { ...} >> >> then x will always be set to [1,2,3], so in your case your >> conditions_by_like function always returns myArray and completely >> discards all the processing you do in the each block. >> Depending on what you want to do you might want to use map/collect or >> just accumulate the result in a local varaible and return that. >> >> Fred > > I understand the logic now. But could you give me an example on how I > would do a map/collect or accumulate it in a variable? I just want to > make sure that if I type in 2 words separated by a space > i:e John Doe both John and Doe will be processed. I''m not a ruby > expert > and just started so I would really appreciate your help and Patience. > Thanksresult = something [1,2,3].each do |x| #do something to result end return result or, if it can be expressed with collect [1,2,3].collect do |x| x*2 end evaluates to [2,4,6] (which I assume you already know since you are using map (which is just an alias for collect) in your code already I''m not sure what you want the final output of your function to be, but i''m guessing that myArray.collect do |itemz| ... end.join('' OR '') If you have more than a small number of rows in your table this will be dog slow since it will require a full table scan each time. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 26 Jun 2008, at 14:52, Sam Ginko wrote: > >>> conditions_by_like function always returns myArray and completely >> expert >> and just started so I would really appreciate your help and Patience. >> Thanks > > result = something > [1,2,3].each do |x| > #do something to result > end > return result > > or, if it can be expressed with collect > > [1,2,3].collect do |x| > x*2 > end > > evaluates to [2,4,6] (which I assume you already know since you areThanx allot! you the man. -- 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 -~----------~----~----~----~------~----~------~--~---