Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't insert on array unless nil

I'm trying to find and record, is the record is nil, I don't want it on my array like this:

@kid <<  Kid.find_only_kid(k) unless nil

But when I get a nil, the record is inserted on the array.

This is the code of my find_only_kid method on my model:

  def self.find_only_kid(kid)
    Kid.where(_id: kid.to_s, parent: false, teacher: false).first rescue nil
  end
like image 448
Jean Avatar asked Dec 14 '25 07:12

Jean


1 Answers

Your code should be :

kid = Kid.find_only_kid(k)
@kid << kid unless kid.nil?

Note:

When a variable name is meant to hold collections, and it should be kids instead of kid. And the method name should be find_only_kids instead of find_only_kid.

like image 172
Arup Rakshit Avatar answered Dec 16 '25 23:12

Arup Rakshit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!