Thursday 1 November 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)
php - file_get_contents shows unexpected output while reading a file
I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...
-
I have an app which needs a login and a registration with SQLite. I have the database and a user can login and register. But i would like th...
-
I would like to use enhanced REP MOVSB (ERMSB) to get a high bandwidth for a custom memcpy . ERMSB was introduced with the Ivy Bridge micro...
-
According to my understanding, and my calculator, cos(90 degrees) equals 0 . In my code, I have a funct...
No comments:
Post a Comment