Controller: @books.collect { |book| book.authors } View: <td> <% for author in book.authors %> <%= h author.name %> <% end %> </td> When I comment out the controller line I get the same result. So should I let the view hit the database on its own or is there some benefit to doing it in the controller? Code in the view cannot be refactored in either situation. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Taylor Strait wrote:> Controller: > @books.collect { |book| book.authors } > > > View: > <td> > <% for author in book.authors %> > <%= h author.name %> > <% end %> > </td> > > When I comment out the controller line I get the same result. So should > I let the view hit the database on its own or is there some benefit to > doing it in the controller? Code in the view cannot be refactored in > either situation.Not sure I understand what you are trying to do but if you wanna list authors that belongs_to book in controlloer def show @book = Book.find(params[:id]) end in view <% for author in @book.authors %> <%= h author.name %> <% end %> www.sylow.net -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-03 13:04 UTC
Re: Am I hitting the database twice?
Hi -- On Sun, 3 Dec 2006, Taylor Strait wrote:> > Controller: > @books.collect { |book| book.authors } > > > View: > <td> > <% for author in book.authors %> > <%= h author.name %> > <% end %> > </td> > > When I comment out the controller line I get the same result. So should > I let the view hit the database on its own or is there some benefit to > doing it in the controller? Code in the view cannot be refactored in > either situation.The controller line appears to discard its result, so I''m not surprised that commenting it out makes no difference :-) Assuming that there''s a line like this in the controller: @books = Book.find(:all) and like this in the view: <% @books each do |book| %> # or an equivalent for loop you could change the controller line to: @books = Book.find(:all, :include => "authors") which would pre-load all the author records into the @books collection and save some database hits later on. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---