Separating an Array into a comma seperated string with quotes
- by user548744
I'm manually building an SQL query where I'm using an Array in the params hash for an SQL IN statement, like: ("WHERE my_field IN('blue','green','red')").   So I need to take the contents of the array and output them into a string where each element is single quoted and comma seperated (and with no ending comma).
So if the array was:   my_array = ['blue','green','red']
I'd need a string that looked like:   "'blue','green','red'"
I'm pretty new to Ruby/Rails but came up with something that worked:
if !params[:colors].nil?
   @categories_array = params[:colors][:categories]
   @categories_string =""
   for x in @categories_array
      @categories_string += "'" + x + "',"
   end
   @categories_string.chop!     #remove the last comma
end
So, I'm good but curious as to what a proper and more consise way of doing this would look like?
Thanks