Variable rails are loaded the first time, and then zero!

I get the following error:

ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?) on line #24 of app/views/index/index.html.erb:
21: <% @achievements.each do |achievement| %>
22:     <%= achievement.name %>
23:     <%= achievement.level %>
24:     by <%= achievement.user.username %><br/>
25: <% end %>

The strange thing is that when the index page loads for the first time, then there is no problem. When I update, I get the error above.

The controller is as follows:

class IndexController < ApplicationController
    def index
        @achievements = Achievement.find(:all)
    end
end

Does this have anything to do with caching? Or is it too much memory? If so, can I load the username differently? I'm confused!

+3
source share
1 answer

Try loading users by adding ": include =>: user" to our search:

class IndexController < ApplicationController
    def index
        @achievements = Achievement.find(:all, :include => :user)
    end
end
+2
source

Source: https://habr.com/ru/post/1760309/


All Articles