Thursday, November 1, 2018
ruby on rails - undefined method `empty?' for nil:NilClass how to avoid it
Answer
Answer
Hi Together I've got this code:
@coursesFound = @user.available_courses
@courses = []
for course in @coursesFound do
@courseInGroups = course.user_groups
for group in @courseInGroups do
@group = UserGroup.find group.id
if @group.users.map { |u| u.id }.include? @user.id
@courses << course
break
end
end
end
# Wenn ein Kurs keiner Gruppe hinzugefügt wurde
if @courseInGroups.empty?
@courses << course
end
on my debian vm it works fine but on my live system I got this error:
undefined method `empty?' for nil:NilClass
How can I avoid this?
Answer
If this @coursesFound = @user.available_courses
returns an empty activerecord relation.
Then this won't execute
for course in @coursesFound do
@courseInGroups = course.user_groups
for group in @courseInGroups do
@group = UserGroup.find group.id
if @group.users.map { |u| u.id }.include? @user.id
@courses << course
break
end
end
end
Which means when you get here @courseInGroups
is nil
if @courseInGroups.empty?
@courses << course
end
So your quick fix would be
if @courseInGroups && @courseInGroups.empty?
@courses << course
end
Subscribe to:
Post Comments (Atom)
plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV
In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...
-
When the left part is an object instance, you use -> . Otherwise, you use :: . This means that -> is mostly used to access instance m...
-
This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programm...
-
I am new to dask and I found so nice to have a module that makes it easy to get parallelization. I am working on a project where I was able ...
No comments:
Post a Comment