views:

12

answers:

0

Using Rails 2.3.8 I've experienced the following problem.

Eager loading works fine When I have a controller like this:

def alternativeview  
  @books = Book.all :include => :author  
  respond_to do |format|  
    format.html { render :action => "index" }  
  end  
end

However, if I want to render this action with a different html.erb e.g. like this:

format.html { render :action => "alternativeview" }  

Then the eager loading doesn't work and I end up with n+1 selects on 'author'.

The other issue is that in my understanding it shouldn't matter if the query then is using eager loading or n+1 queries if both html.erb files are the same, like this:

 <% @books.each do |book| %>
    <%=h book.author.name %>
 <% end %>

But this only works if my above example is rendering index.html.erb, but when rendering alternativeview.html.erb then book.author is nil.

edit I also tried

  format.html { render :template => "books/index" }

vs

  format.html { render :template => "books/alternativeview" }

or leaving the whole { render ...} block out so it will render the default for the action, but with the same result. only index works.