I have a problem with an "each do"-loop and I don't know what the cause of it is.
Rails version: Rails 5.2.1 Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Here is my code:
<%= @course.sessions.each do |session| %>
<%= session.topic %><br>
<hr>
<% end %>
@course.sessions.count is 0. This course doesn't have any sessions. Yet, there is still something there:
<%= @course.sessions.each do |session| %> # shows:
[#<Session id: nil, topic: nil, date: nil, description: nil, course_id: 1, created_at: nil, updated_at: nil>]
When I add a session to the course, the problem remains:
<%= @course.sessions.each do |session| %> # shows:
[#<Session id: 1, topic: "First Session", date: "2018-09-14", description: "First Session", course_id: 1, created_at: "2018-09-14 13:29:30", updated_at: "2018-09-14 13:29:30">, #<Session id: nil, topic: nil, date: nil, description: nil, course_id: 1, created_at: nil, updated_at: nil>]
The "each do"-loop also occurs twice. See this picture
@course.sessions.count: <%= @course.sessions.count %><br>
<%= @course.sessions.each do |session| %>
<hr>
Topic: <%= session.topic %><br>
<hr>
<% end %>
What seems to be the problem?
EDIT:
Here is my controller:
def show
@new_session = @course.sessions.build
end
EDIT2:
The problem also occurs when using <% .. %>
@course.sessions.count: <%= @course.sessions.count %><br>
<% @course.sessions.each do |session| %>
<hr>
Topic: <%= session.topic %><br>
<hr>
<% end %>
The loop occurs twice, although there is only one session in it.