1

so i have this code that and my aim was to convert any empty string to null

def convert_empty_strings_to_null
  if request.patch? || request.post?
    convert_empty_strings_to_null_rec(request.params)
  end
end

def convert_empty_strings_to_null_rec(param)
  param = nil if param.empty? if param.is_a?(String)
  param.all?{|v| convert_empty_strings_to_null_rec v} if param.is_a?(Array)
  param.all?{|k,v| convert_empty_strings_to_null_rec v} if param.is_a?(Hash)
end

But i'm new to ruby on rails and i found it that it sends params by value and not by reference, so no change in params is made, how do i fix this ?

1
  • Note that all? does not replace anything. Use map instead. In case of the hash, use param.map{ |k,v| [x,y] }.to_h. Commented Jun 28, 2016 at 7:44

2 Answers 2

3

I assume that by "empty" you mean zero-with strings, meaning that strings consisting only of whitespace should be left intact. (Otherwise blank? and strip would be your friends.)

def convert_empty_strings_to_nil
  if request.patch? || request.post?
    request.params.each do |key, value| 
      request.params[key] = convert_empty_strings_to_nil_rec(value)
    end
  end
end

def convert_empty_strings_to_nil_rec(param)
  case param
  when String
    param.empty? ? nil : param
  when Array
    param.map{ |v| convert_empty_strings_to_nil_rec(v) }
  when Hash
    param.map{ |k,v| [k, convert_empty_strings_to_nil_rec(v)] }.to_h
  else
    param
  end
end
Sign up to request clarification or add additional context in comments.

4 Comments

This would fix the problem but apparently the operator '=' isn't defined for params, is there a way to iterate over params and change the elements themselves without creating a new object?
There are two ways: (1) Store the returned modified params hash in a new instance variable and use these. (2) Yes, knowing that request.params is always a hash, we can modify its values in-place. Should I elaborate?
Thanks i solved it by calling the recursive call on the first level of request.params for each value
Cool! I updated the answer accordingly. Feel free to mark it as the accepted answer anytime :)
3

First of all, this is how your convert_empty_strings_to_null_rec method should be, for keeping the changes persistent:

def convert_empty_strings_to_null_rec(param)
      if param == ""
        updated_param == nil
      elsif param.is_a?(Array)
        updated_param == param.map{|x| nil if x.empty? }        
      elsif param.is_a?(Hash)
        updated_param = {}
        param.each do |k, v| 
            if v.empty?  
                updated_param[k] = nil
            else
                updated_param[k] = v
            end
        end
      end
      return updated_param
end

Further, I am assuming from your question that convert_empty_strings_to_null is a action method. It should be updated to catch what convert_empty_strings_to_null_rec method is returning.

def convert_empty_strings_to_null
  if request.patch? || request.post?
    updated_params = convert_empty_strings_to_null_rec(request.params)
  end
  # you can use the updated_params here on in this action method
end

Hope it helps : )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.