ActiveRecord search Query Question

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

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

You're not returning the value you think you are (each always returns
the collection you search for).

Fred

Frederick Cheung 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

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. 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 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

Frederick Cheung wrote: