Ruby error: undefined method `casecmp' for nil:NilClass - ruby-on-rails

I have the following code (with a few debug lines added):
Ruby:
re_dict = {}
re_dict['state'] = 'pending' #set initial status to pending
puts re_dict, re_dict.class.to_s
puts re_dict['state'], re_dict['state'].class.to_s
puts re_dict['state'].casecmp('pending')
while re_dict['state'].casecmp('pending') == 0 do
stuff
end
Output
state: pending
state class: String
class compared to 'pending': 0
Completed 500 Internal Server Error in 66ms
NoMethodError (undefined method `casecmp' for nil:NilClass):
What is causing this? How am I losing the value of my hash?

This will happen only when you remove 'state' key from re_dict hash inside your while loop:
while re_dict['state'].casecmp('pending') == 0 do
puts re_dict
re_dict = {}
end
#=> {"state"=>"pending"}
#=> NoMethodError: undefined method `casecmp' for nil:NilClass
Since, key 'state' is not available anymore, calling re_dict['state'] will give nil, that's why you're getting undefined method casecmp' for nil:NilClass

Related

Undefined local variable but the variable is defined

In a Rails project, I have following error:
undefined local variable or method ` response' for #<Deliveries::CheckJobService:0x00007fce8548dd60> Did you mean? response
And here is the code:
delivery_status = response['status']
I don't see the error
I try new things, and the error is even weirder:
def call
return false if #order.stuart_job_id.nil?
response = stuart_check_job
if response.nil?
#order.update(delivery_status: 'sth went wrong')
else
delivery_status = 2
delivered_at = 4
#order.update(delivery_status: delivery_status, delivered_at: delivered_at)
end
return true
end
This is the error:
undefined local variable or method ` 2' for #<Deliveries::CheckJobService:0x00007fce8490fc40>
I don't see any single quotation

rspec test fails code in rails controller

In my application_controller.rb, i have a line of code as follows:
def index
CaseStatus.order(:application_source).pluck(:application_source).uniq!
end
In my rspec code, i have a line of code that visits the index path of application_controller as follows
visit applications_path
When i run the code directly, it works perfectly but when it visits application_controller.rb via rspec, i get an error which says
NoMethodError:
undefined method `compact' for nil:NilClass
Not sure while i get this error via rspec and capybara but if i run the code as
def index
CaseStatus.order(:application_source).pluck(:application_source)
end
It executes perfectly with no errors. Kinda confused what the uniq! breaks in the code that suddenly the result becomes nil.
i get this error
Failure/Error: #application_channels = CaseStatus.order(:application_source).pluck(:application_source).uniq!.compact if CaseStatus.order(:application_source).present?
NoMethodError:
undefined method `compact' for nil:NilClass
# ./app/controllers/loan_applications_controller.rb:53:in `index'
I do not think uniq! is the method you would like to use in this case, see:
Returns nil if no changes are made (that is, no duplicates are found).
https://ruby-doc.org/core-2.2.0/Array.html#method-i-uniq-21
So it works like this:
2.3.1 :008 > a = [1,2,3,3,nil].uniq!
=> [1, 2, 3, nil]
2.3.1 :009 > a = [1,2,3,nil].uniq!
=> nil
2.3.1 :010 >
on the other hand uniq works like:
2.3.1 :010 > a = [1,2,3,3,nil].uniq
=> [1, 2, 3, nil]
2.3.1 :011 > a = [1,2,3,nil].uniq
=> [1, 2, 3, nil]
and on the output of uniq it is safe to run compact to remove nil values.

Rails 5: ActiveRecord collection index_by

Upgraded app to Rails 5 using the index method. The issue is that it is not incrementing to the next ActiveRecord collection record. The below code below use to work in Rails 4.0. Tried with index_by.
def next_question
index = campaign.quiz_questions.index self
campaign.quiz_questions[index + 1]
end
Debugger
(byebug) campaign.quiz_questions.index
*** NoMethodError Exception: undefined method `index' for #<QuizQuestion::ActiveRecord_Associations_CollectionProxy:0x007f80012d71b0>
Did you mean? index_by
Using index_by
(byebug) index = campaign.quiz_questions.index_by
#<Enumerator: #<ActiveRecord::Associations::CollectionProxy [#<QuizQuestion id: 113, campaign_id: 492, message: "Where did Hullabalooza's freak show manager send H...", created_at: "2016-07-20 20:50:32", updated_at: "2016-07-20 20:50:32">]>:index_by>
Index + 1
(byebug) index + 1
*** NoMethodError Exception: undefined method `+' for #<Enumerator:0x007fc4db445960>
nil
changed it to find_index method. Now it's working
def next_question
index = campaign.quiz_questions.find_index self
campaign.quiz_questions[index + 1]
end

Why does undefined method `-#' for nil:NilClass mean (haml)?

I have this rails view partial and I just can't see the issue:
%span{class: "show_hide #{show_hide}"}
---------------------
= first_field_focus
---------------------
- if first_field_focus == "1"
- puts '++++++++++++'
- puts 'y'
- puts '.............'
- puts '.............'
- puts '==='
%a{href: '#', :data => {toggle_description_length: 'toggle'}} # Line 11
= raw(txt)
but I just keep getting:
NoMethodError in Links#index
Showing /home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_toggle_details_link_bold.html.haml where line #11 raised:
undefined method `-#' for nil:NilClass
Extracted source (around line #11):
...
It means you're trying to call the - operator on something that is nil. Probably those --------------------- lines are being interpreted as a code line that's a chain of minus signs and it's confusing things. Try making it \--------------------- instead.

Object appears as existing and not at the same time

I've got some very strange issue which I cannot debug.
This is my code:
def find_order_and_payment
#payment = Spree::Payment.find_by_identifier(params['session_id'])
Rails.logger.info "payment_id #{#payment.id}"
#order = #payment.order
end
And this is the output:
I, [2014-01-17T16:39:43.084827 #12342] INFO -- : payment_id 187
I, [2014-01-17T16:39:43.090718 #12332] INFO -- : Completed 500 Internal Server Error in 448ms
NoMethodError (undefined method `id' for nil:NilClass):
app/controllers/payu_status_controller.rb:36:in `find_order_and_payment'
Line 36 is the line with Rails.logger. I don't understand, why I get correct id, but same line returns undefined method id? If I'll call above code from console everything works as expected.

Resources