I have a controller:
class UsersController < ApplicationController
def departments
@users_departments = current_user.departments
@new_department = current_user.department.new
end
My view looks similar like this:
<%= form_for @new_department, :url => {:action => "departments"} do |f| %>
.
<% end %>
<% @users_departments.each do |dept| %>
<td><%= dept.name %></td>
<td><%= dept.employees %></td>
<% end %>
@users_departments.each... shows me an empty department. Why? And how to solve that?
Ok I found out that the magic thing is called lazy and eager loading.
What I made was changing this:
to
Rails seems to be loading the results in the view if it was called. With "find" in place the db get hit and
@users_departments
fetches "really" the entries. Not in view, like before.