Home > Blockchain >  Using Find Method on an already existing Find Method in Ruby showing NoMethodError
Using Find Method on an already existing Find Method in Ruby showing NoMethodError

Time:02-23

As the question says, I am trying to use the .find Method but I am getting a NoMethodError for Nil Objects.

I understand that this is normal behaviour it makes sense, but is there anyway in which I could improve the code to avoid using the .find twice?

This is how my code looks currently:

@device = Device.find(:all).find{|device| device.uuid == @token.uuid}

RubyMine is highlighting the second .find method:

.find{|device| device.uuid == @token.uuid}

with a warning saying :

Method invocation 'find' may produce 'NoMethodError'

I realise this may not be the best way to go about this. Can anyone please help on suggestions? And maybe tell me whether by removing the second .find() I'd get the same results!

I kindly appreciate all answers.

CodePudding user response:

Try this way

@device = Device.find(:all, :conditions => { :uuid => @token.uuid })
  • Related