Ruby on Rails 4 - How do I iterate through these params? - ruby-on-rails

I have the following params that are being submitted:
{"utf8"=>"✓",
"course"=>
{
"title"=>"Test", "roles"=>["Tuesday", "Thursday"]
}
}
My question is: How do I iterate through the "roles" params to "add_role"?
I am currently trying this iteration, but it is not working:
#course.user_id = current_user.id
params['course']['roles'].each do |key, value|
#course.add_role(key['course']['roles'])
end
I get the following error: undefined method '[]' for nil:NilClass
What am I doing wrong with iterarting the params?
Thank you!

params['course']['roles'] is the array ["Tuesday", "Thursday"], not a hash.
In your loop, key takes the value of a role string, "Tuesday"['course'] returns nil and nil['roles'] causes your error.
You seem to be trying to do this:
params['course']['roles'].each do |role|
#course.add_role(role)
end

Related

undefined method `map' for #<ActionController::Parameters Rails 6.0

Getting this error when trying to call the method validate_create_action. The error occurs on the line #element = klass.new(params[:element]).
NoMethodError in Analyses::MitigationMatricesController#validate_create_action
undefined method `map' for #<ActionController::Parameters {"0"=>{"phase"=>"Launch", "reason"=>"test"}} permitted: false>
Did you mean? tap
Extracted source (around line #7):
#5 class ManyEmbeddedProxy < EmbeddedCollection
#6 def replace(values)
*7 #_values = (values || []).compact.map do |v|
#8 v.respond_to?(:attributes) ? v.attributes : v
#9 end
#10 reset
In the post request the element object below is sent to the backend. The issue seems to be the array critical_phases.
element[applicable_during][critical_phases][0][phase] "Launch"
element[applicable_during][critical_phases][0][reason] "test"
If I add the method to_unsafe_h to params[:element].to_unsafe_h I get a different error involving the critical_phases array saying "TypeError: no implicit conversion of String into Integer". Any ideas on how to solve this?
element_controller.rb
def validate_create_action
rejigger_dynamic_lists
remove_associations_from_params
#element = klass.new(params[:element])
...
def klass
unless #klass
klass_str = params[:controller].split("/").last.singularize.camelize
namespace_str = SITE_CONFIG['adaptation_name'].camelize
#klass = "#{namespace_str}::#{klass_str}".constantize
end
#klass
end
UPDATE:
I updated my code to the working code
params[:element].permit!
params2 = ActionController::Parameters.new(params[:element][:applicable_during][:critical_phases].to_h)
params2.permit!
params[:element][:applicable_during][:critical_phases] = params2.values.map do |h|
ActionController::Parameters.new(h.permit!.to_h).permit(:phase, :reason) # use permit! instead if you want to live dangerously
test_mapping = {}
end
The problem here is that it the parameters aren't actually an array. Its a hash with the keys "0", "1" etc. This is done to avoid the potential ambigiuty thats happens when Rack parses form data keys containing arrays of hashes:
irb(main):015:0> Rack::Utils.parse_nested_query("foo[][bar]=1&foo[][bar]=2")
=> {"foo"=>[{"bar"=>"1"}, {"bar"=>"2"}]}
# should this be one or two hashes? Discuss
irb(main):016:0> Rack::Utils.parse_nested_query("foo[][bar]=1&foo[][baz]=2")
=> {"foo"=>[{"bar"=>"1", "baz"=>"2"}]}
irb(main):017:0> Rack::Utils.parse_nested_query("foo[0][bar]=1&foo[1][baz]=2")
=> {"foo"=>{"0"=>{"bar"=>"1"}, "1"=>{"baz"=>"2"}}}
Typically normalizing this is handled by accepts_nested_attributes_for. But what you're doing doesn't look the least bit ordinary.
If you want to jury rig a system that processes this and returns an array of whitelisted hashes you could do:
params = ActionController::Parameters.new("0"=>{"phase"=>"Launch", "reason"=>"test"}, "1"=>{"phase"=>"Foo", "reason"=>"bar"})
params.values.map do |h|
ActionController::Parameters.new(h)
.permit(:phase, :reason) # use permit! instead if you want to live dangerously
end
# [<ActionController::Parameters {"phase"=>"Launch", "reason"=>"test"} permitted: true>, <ActionController::Parameters {"phase"=>"Foo", "reason"=>"bar"} permitted: true>]

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\x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BC\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.

undefined method `take' for #<ActiveRecord::QueryMethods::WhereChain:0x00000107609598>

I am trying to select all profiles where a.blocked?(p) returns false!
However I am getting an error.
in a method in model
p = self.find_by_email(email)
all.find_by { |a| a.blocked?(p) }.order('random()')
error
undefined method `take' for #<ActiveRecord::QueryMethods::WhereChain:0x00000107609598>
find_by does not take a block, but rather conditions in the form of a hash, array, or string. See the documentation: http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
If you can't replicate blocked? in SQL, you'll have to do the filtering in Ruby like:
all.find_all { |a| a.blocked?(p) }.shuffle
Because that's doing the filtering and sorting in Ruby rather than SQL, it will be slower.

How to catch an "undefined method `[]' for nil:NilClass" error?

I get an nested array from facebook via omniauth and wanna check if it's empty?/nil?/exists?
the depending line looks like:
unless omniauth['extra']['raw_info']['location']['name'].nil?
This should check if this part of the array is empty or exists.
But always this error was thrown:
undefined method `[]' for nil:NilClass
Do I check arrays wrong?
I tried it with "has_key" "nil?" "empty?" "exists?" "blank?"
But no one of these works!
Please help me, many thanks in advance!
Ideally you should check each nested level to see if it is nil, however, this will also work.
unless (omniauth['extra']['raw_info']['location']['name'] rescue nil).nil?
You can also rescue the NoMethodError specifically.
This error is raised because one of the hash values in the chain of omniauth['extra']['raw_info']['location']['name'].nil? returns nil and it is not the last call ['name'].
If for example omniauth['extra']['raw_info'] returns nil, you're actually trying to call nil['location'] which raises an error in ruby.
You can catch this error simply:
res = omniauth['extra']['raw_info']['location']['name'].nil? rescue true
unless res
#your code here
end
Please notice that the code block above will fill the variable res with true if the ['name'] hash value is nil or any other hash value in the chain returns nil.
A bit late to the party, but, as pointed in this answer, Ruby 2.3.0 introduced a new method called dig, which would return nil if one of the chained keys is nil. Your omniauth auth hash could then be presented as:
omniauth = {
...
"extra"=>{ "raw_info"=>
{ "location"=>"New York",
"gravatar_id"=>"123456789"}}
...
}
omniauth.dig('extra',
'raw_info',
'location',
'name',
'foo',
'bar',
'baz') #<= nil

Ruby - how to get and loop through all the values in an object returned from the db?

I have this line:
#users = database['users'].find(:all).limit(10)
it returns this object:
<Mongo::Cursor:0x8759a858 namespace='app-development.users' #selector=:all #cursor_id=>
and what I thought was that it has a collection of the 10 users it got from the database. Is that correct? I tried iterating through it like this:
#users.each {
|info|
logger.debug ".....................single user: " + info.inspect
}
but I got this error:
BSON::InvalidDocument (BSON.serialize takes a Hash but got a Symbol):
Help much appreciarted!
I'm not framiliar with mongo but try this:
#users.each do |info|
logger.debug ".....................single user: #{info.inspect}"
end
The #{} allows you to evaluate ruby code within a double quoted string.
If you want to see the litteral result try appending to_a to the query. This should give you an array with ten items in it.
#users = database['users'].find(:all).limit(10).to_a

Resources