I am currently working with Ruby on Rails and I encountered a weird thing.
So, I am creating an array, then using .pluck(:client_id)
on that array and I get the following error: NoMethodError: undefined method 'pluck' for nil:NilClass
.
I used binding.pry
to check if the array is indeed nil, but this is the result:
49: def get_associations(records, tuples)
=> 50: binding.pry
51: ClientAssociatedLocation.with_deleted.
52: where(client_id: records.pluck(:client_id)).
53: where("CONCAT_WS('-', client_id, location_id) IN (?)", tuples)
54: end
[1] pry(#<AssociatedLocations::SourcesCreator>)> records
=> [{:id=>nil, :user_id=>nil, :client_id=>1523, :location_id=>2257, :association_type=>"manual"}]
[2] pry(#<AssociatedLocations::SourcesCreator>)> records.pluck(:client_id)
=> [1523]
[3] pry(#<AssociatedLocations::SourcesCreator>)>
As you can see, the records is clearly not nil and the pluck works. Do you have any idea of why this occurred?
Change this:
def get_associations(records, tuples)
binding.pry
ClientAssociatedLocation.with_deleted.
where(client_id: records.pluck(:client_id)).
where("CONCAT_WS('-', client_id, location_id) IN (?)", tuples)
end
To this:
def get_associations(records, tuples)
binding.pry if records.nil?
ClientAssociatedLocation.with_deleted.
where(client_id: records.pluck(:client_id)).
where("CONCAT_WS('-', client_id, location_id) IN (?)", tuples)
end
Then re-run the operation as many times as it takes to trigger the binding. The problem is that records
is nil
some of the time. You'll need to figure out the conditions that cause it by trapping only that issue.
Realistically, you should have the binding.pry
call placed somewhere lower in the stack. Find wherever you call get_associations(records, tuples)
and put in binding.pry if records.nil?
there instead, so that you can see the context in which it's called.
You've tagged this question with both Ruby on Rails 3 and Ruby on Rails 4 so I assume you're using some very old versions of Ruby and Rails. If you weren't then I'd recommend using debug over pry to make the debugging a little easier.