no implicit conversion of Symbol into Integer while passing params - ruby-on-rails

i want to pass model name to prams[:required_model] but it's giving me conversion error
please let me know how this works
def method obj
required_model = obj.class.model_name.human
params[:required_model] = required_model
model_values_attributes = params[:required_model][:model_values_attributes]
model_values_attributes.each do |key, value|
if value[:value].blank?
field_values_attributes.delete(key)
else
value[:company_id] = current_company.id
end
end
end
when I do params[:property][:model_values_attributes] this works perfect so I want to pass model_name to whatever is in params[:ABC]

That error means you're trying to access an element X from an object that responds to [] using symbols instead of using integers;
"hej"[2]
# => "j"
[1, 2, 3][0]
# => 1
[1, 2, 3][:foo]
# in `<main>': no implicit conversion of Symbol into Integer (TypeError)
So this line params[:required_model][:model_values_attributes] might be failing because required_model is a string (or an array) and not a hash. Or just because passing symbols to [] is not implemented.

Related

Hash collect Ruby23 --> Ruby27 and Rails 5.2 --> Rails 6.1 giving wrong element type nil at 0 (expected array)

I am upgrading Ruby and Rails version for some code package.
I have the following code block that is giving me error of wrong element type nil at 0 (expected array) .
def some_method(var1)
address_to_field_mapping = { 'name' => 'name'
'countryCode' => 'countryCode'
}
# following line errors:
fields = Hash[address_to_field_mapping.collect{|k,v| [v, var1[k]] unless var1[k].nil?}]
return {'fields' => fields}
end
I am not familiar at all with Ruby/Rails, can someone explain what the highlighted line means , and what is actually causing this error ? fields = Hash[address_to_field_mapping.collect{|k,v| [v, var1[k]] unless var1[k].nil?}]
This looks like an insanely overcomplicated way of writing:
def some_method(var1)
{ 'fields' => var1.slice('name', 'countryCode').compact }
end
This returns a hash containing the 'name' and 'countryCode' fields if they are not nil. Or at least it would have been if it wasn't broken.
I'm guessing the person writing it didn't know Ruby (hinted by the unnessicary return) yet was very concerned with job security.
So lets break it down:
address_to_field_mapping.collect{|k,v| [v, var1[k]] unless var1[k].nil?}
Hash#collect is an alias of map and loops across the keys and values of an array and returns an array consisting of the return values of the block. The return value here is an array of pairs ['name', 'bob'] or nil. The latter part being where it goes tits up.
Hash[] is a special hash constructor that takes an array of pairs:
irb(main):001:0> Hash[[[foo: 1], [bar: 2], [baz: 3] ]]
=> {{:foo=>1}=>nil, {:bar=>2}=>nil, {:baz=>3}=>nil}
But if you throw a nil in there it blows up.
irb(main):002:0> Hash[[[foo: 1], [bar: 2], nil ]]
(irb):2:in `[]': wrong element type nil at 2 (expected array) (ArgumentError)
Which could have been fixed by using .compact to remove the nils:
def some_method(var1)
address_to_field_mapping = { 'name' => 'name'
'countryCode' => 'countryCode'
}
fields = Hash[address_to_field_mapping.collect{|k,v| [v, var1[k]] unless var1[k].nil?}.compact]
{'fields' => fields}
end

JSON no implicit conversion of String into Integer in Ruby

I'm grabbing a JSON hash from my server and trying to read it in my ruby script. It seems to be returning the wrong value though, unless I'm just loosing my mind:
I'm calling this from a function:
def get_functions(serial_number)
response = HTTParty.get("#{get_host_url}/v1/devices/#{serial_number}.json")
data = response['scan_option']
return data
end
Here is the returned JSON:
{"can_scan"=>true, "can_brute_ssh"=>false, "can_brute_telnet"=>false, "can_brute_voip"=>false, "can_brute_smtp"=>false, "can_brute_pop3"=>false, "can_google_crawl"=>false, "can_scan_external_ip"=>false, "scan_ip_list"=>["10.10.10.1"], "exclude_ip_list"=>[]}
Which is then read into the following code:
data.each do |d|
#can_scan = d['can_scan']
# ....
end
However this is throwing an error:
no implicit conversion of String into Integer
{foo: :bar}.each do |d|
p d
d['meow']
end
# => [:foo, :bar]
#! TypeError: no implicit conversion of String into Integer
Hash#each yields a two element array ([key, value]). You then try to index that array with d["can_scan"], which fails as arrays can only be indexed with integers.
Instead, directly access the value - data['can_scan'].
I you mean that data in your third snippet (where you call data.each) is the hash mentioned just above it, indeed that would be troublesome. Calling each on a hash will itterate over its key, value pairs, giving you an array in the block var d of the data.each (with a [key, value] pair in it).
You might just want to call data['can_scan'].
Note that the return at the end of your method defenition is not needed in Ruby. You can just do:
def get_functions(serial_number)
response = HTTParty.get("#{get_host_url}/v1/devices/#{serial_number}.json")
response["scan_option"]
end

no implicit conversion of Symbol into Integer, rails 4

I have been getting this error, no implicit conversion of Symbol into Integer. I searched for this error but don't really understand it. Code is at the bottom. Once it gets to this "if q[:text_field].is? Array" that is when it gives the error and I'm sure that the rest of that code is wrong. But no idea how to fix it.
pages = Vovici::API.new(#pid).survey_structure
This is a sample of the api data that I'm calling with the code above.
[{:q_fill_in=>
{:heading=>{:text=>"1"},
:instructions=>{:text=>nil},
:body=>{:text=>"Pac"},
:list_caption=>{:text=>{:#type=>"plain"}},
:total_label=>{:text=>"Total"},
:text_field=>
[{:label=>{:text=>"first"},
:preselection=>{:text=>{:#type=>"plain"}},
:symbol=>{:text=>{:#type=>"plain"}},
:#id=>"1",
:#dbheading=>"Q1_1",
:#row=>"0",
:#size=>"20",
:#xmltype=>"text",
:#required=>"false",
:#compare_expression=>"-1",
:#topic_first=>"true",
:#slider=>"false",
:#sliderstep=>"1",
:#published=>"true",
:#usecalendarpopup=>"true",
:#insert_symbol_left=>"false",
:#label_width=>"3",
:#text_area_width=>"9"},
{:label=>{:text=>"id"},
:preselection=>{:text=>{:#type=>"plain"}},
:symbol=>{:text=>{:#type=>"plain"}},
:#id=>"2",
:#dbheading=>"Q1_2",
:#row=>"0",
:#size=>"20",
:#xmltype=>"text",
:#required=>"false",
:#compare_expression=>"-1",
:#topic_first=>"true",
:#slider=>"false",
:#sliderstep=>"1",
:#published=>"true",
:#usecalendarpopup=>"true",
:#insert_symbol_left=>"false",
:#label_width=>"3",
:#text_area_width=>"9"}],
:#dbheading=>"Q1"}
This is code from my rb file
def process
pages = Vovici::API.new(#pid).survey_structure
pages.each do |page|
if page[:q_fill_in]
process_fill_in(*page[:q_fill_in])
end
end
end
def process_fill_in(*questions)
questions.each do |q|
if q[:text_field].is? Array
sub_qs = q[:text_field]
else
sub_qs = [q[:text_field]]
end
q_text = clean_question_text(q[:body][:text])
sub_qs.each do |sq|
sub_text = clean_question_text(sq[:label][:text])
q_name = [q_text, sub_text.reject { |i| i.nil? || i.empty? }.join("--")]
#survey.questions.create!(qheader: sq[:#dbheading], qtext: q_name)
end
end
end
def clean_question_text(text)
match = /( )?(<.*?>)?(.+)( )?(<\/.*>)?/.match(text)
match[3]
end
Can anyone please help?
This error means that you've used [] on an array but you've passed something that doesn't make sense to an array. In this particular case it's telling you that q which you are trying to use as a hash is in fact an array.
This is happening because process_fill_in(*page[:q_fill_in]) is turning the hash into an array of key-value pairs (because of the *). I'm not sure why you've got a splat there at all.

Ruby loop through hash and run function

I'm trying to loop through this hash, and use Zlib.inflate() function to decompress the values of each of the given keys, then print the output in JSON.
{"event_id"=>1, "a_full"=>"x\x9C\xD3\xD5M\xB4L40\xB10N\xD4u\xD4\xD5\xE5\x8A64\xD2w,(\xD27204\xB320\xB326\xB226W\xD0\xD55010\x88U\b+\xAF\xF2\xAF\xF0\xB0pttt\x05b\xA3\"\x97\xFCr \xED\xE8\xA6`hn\xA4gh\x01A\xC6\n\xA6\x86\x96\xA6F\xC8b\x86\xE6\n\x16\x06\\\x00cf\x18\x88", "b_full"=>"x\x9C]\x8F\xCBn\xC20\x14D\xF7\xF9\x8A\xBB,\xD4\x8F\x84#\nA]P\x84\xA0\x02\xDA\xAA\x04em\x9CK\xB0pl\x93\x84\x87\xF8\xFA\x06J7\xDD\x8D\x8Ef43\x94\x8A\x81\xF0\xBB\xFDP\xD07J\xBD\xE9$\x01\x0E\xB3$\xF9\xE2\x01\v\xBC\x99\xAD\xEA\x18j\xACj\xB7s\xECt\xD4\xE6\x8C\e&m\xE1\x8D\xAD1(keM\f{DG\x85V'\xF4FR\xA2\xBBG.5\xDF\xD5\x85&\xC29\xAD\xA4\xB89\xF9\xE5F\x9E/\xFFi\xA1\x87\x87W\x9F\r\x88*D\x8E\xBC\xA9p\xA4\xCD\xDBw\xD8\xF7\xD6./E\x86\xF4\xDDT(\x8F%\xD2o<\x1C\x9BEU\f\x81\xB7\xAE\xB0\xA4\xA3\x1CM\xD3\xB9\xB4W\xA5\xB5\xE0=\xE6\xC3S\xAALf\xCF\x15|$\x10\xB1p\b\xE9g\x1Au[0jz1\xC5\xCD\\\x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x95(\xC8\xA4\x84\xDBE\x9Cc`\n\x8Cv}\x91\x06\xA67\xA7\x1D?\x87\x03\xF8\xCE\x8D\x13\xF1\xF1n+,\xC5\x03M\\h\xA4\x06O\xAB\x06S\x9Bt\x9B\xE4\x89\x92\xA8>\xC3\xEC\xFBf\x93\xCD\xCB\xD4\xF6\xD9\xFF\xBBRh\xB9\xD3P\xC8\xFD\xEE\x87\x81<\xF71h\xF0C\x9CH\x88/'s.\"", "h_full"=>"x\x9Cu\x90Mj\xC30\x10F\xF7:\xC5\x90U\f\x91mYr\x12\xBBh\x914\x86,\xFAG\xDD\v\xA8\xD24\x0E\x8D%#\xDBus\xFB*i!\xAB\"x\x1A\x867\f\xDFP\xAA\n\x95\x8A5WtO)\xD9tJ7H\xF7\xCA\x9A\x13\xFA\x12:\xEF\xBE\xCF\xB4G\xFF\x85\x9E\xD4\x83\xEB&5\xE8\xA6\x04&\x96\xA9\xC8W,_\xAEx\x911\x06\"-x!`N!\xBC\xE8\xA6f\xFF\xBAw\xA0]\xFB~\xB4h$\xE7\xD9\x02:&\xD7\x81\x99\xE4\xE9*\xFC\\\xB2#qe.\x99X#\xEFe\x1A8]Z\xA7Ky\xD02%\xAF\xD8w\xCE\xF6H\xB7\xCE\x9C\xE9\x9BW\xB6\xFFp\xBEES\xC2\x0Eu3\xDAO4\xE4\xC5;3\xEAK\xA0Ggj\xD4\xA3?\x0Eg\b\x1E\xFC\x06N\xB2\xB8\x88\x19\xCC\x9Ba\xE8\xCA$\x99\xA6)n\x9D\xE9\xFF\xC4\xD8\xF9C\x12\xC5\xA4\xBE\x9E\xA1\xBC\xCDd1\xCBa~\x8Fvx\xAE#R\xD9CHC\xC3\x06,aV=m\xB6\x0F\xD5nF\xC8\x0Fa\x10g\xFE", "i_full"=>"x\x9C\x03\x00\x00\x00\x00\x01", "k_full"=>"x\x9C\x03\x00\x00\x00\x00\x01", "z_full"=>"--a9a0483a-Z--\n", "compressed"=>true}
Here is a Pastebin link
When I tried using .each method, I got NoMethodError: undefined method 'data' for main:Object
My goal is to loop through this hash, and pass values to Zlib.inflate(a_full), then b_full ..etc
Your help is highly appreciated
p h.map{|k,v|[k,Zlib.inflate(v)] rescue[k,v]}.to_h.to_json
Assuming you're using Rails, this should work:
hash = { event_id: 1, a_full: 'asdadadsd', b_full: 'asdfasdfsdf' }
hash.except(:event_id).each do |key, value|
Zlib.inflate(value)
end
If you wanted to preserve the keys in the hash and convert the deflated result to JSON, you can do something like:
hash = { event_id: 1,
a_full: 'asdadadsd',
b_full: 'asdfasdfsdf',
z_full: 'dddsds',
compressed: true }
hash.each_with_object({}) do |(key, value), result|
result[key] = key.to_s =~ /_full/ ? Zlib.inflate(value) : value
end.to_json
# => "{\"event_id\":1,\"a_full\":\"deflated asdadadsd\",\"b_full\":\"deflated asdfasdfsdf\",\"z_full\":\"deflated dddsds\",\"compressed\":true}"
This example deflates all values in the hash of the *_full keys and leaves the rest untouched. At the end, the resulting hash is converted to JSON.

How to convert a find_by_sql hstore string to a hash in Ruby on Rails

This seems ludicrously simple but I cannot figure out how to convert a hash-string to a hash.
When I do a Answer.find_by_sql I get a string like this
deepthought = "\"answertolife\"=>\"42\""
But I cannot figure out how to turn that into a hash.
I have tried:
pry(main)> Hash[deepthought]
ArgumentError: odd number of arguments for Hash
pry(main)> JSON.parse deepthought
JSON::ParserError: 757: unexpected token at '"answertolife"=>"42"'
pry(main)> deepthought.to_json
=> "\"\\\"answertolife\\\"=>\\\"42\\\"\""
I saw How do I convert a String object into a Hash object?, but I still cannot figure it out.
Try this
eval("{ #{deepthought} }")
It wraps the deepthought string with curly brace { }, and then use eval
A bit late but if you need to convert a multiple entries this works great.
def hstore_to_hash(hstore)
values = {}
hstore.gsub(/"/, '').split(",").each do |hstore_entry|
each_element = hstore_entry.split("=>")
values[each_element[0]] = each_element[1]
end
values
end
Rails4 supports hstore out of the box so I'd probably handle the string casting the same way Rails4 does it. If you look inside the Rails4 PostgreSQL-specific casting code, you'll find string_to_hstore:
def string_to_hstore(string)
if string.nil?
nil
elsif String === string
Hash[string.scan(HstorePair).map { |k, v|
v = v.upcase == 'NULL' ? nil : v.gsub(/\A"(.*)"\Z/m,'\1').gsub(/\\(.)/, '\1')
k = k.gsub(/\A"(.*)"\Z/m,'\1').gsub(/\\(.)/, '\1')
[k, v]
}]
else
string
end
end
and a little lower down in the same file, you'll find HstorePair:
HstorePair = begin
quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
unquoted_string = /(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
/(#{quoted_string}|#{unquoted_string})\s*=>\s*(#{quoted_string}|#{unquoted_string})/
end
Stash that somewhere convenient (probably somewhere in lib/) and send your hstore strings through that string_to_hstore to unpack them into Hashes.
This seems to work but feels dirty.
JSON.parse "{ #{deepthought} }".gsub('=>', ':')

Resources