Hi, I''d like to be able to query a ''result set'' without hitting the db again for example: In my controller: Def show #load somel widgets @widgets = Widgets.find_all(''type = 1 or type = 2'') End In my view: <% @type1 = @widgets.find_all(''type = 1'') %> <% @type2 = @widgets.find_all(''type = 2'') %> I tried this in Rails 12.0 but additional db hits were created. Thanks, Nev
Neville Burnell wrote:><% @type1 = @widgets.find_all(''type = 1'') %> ><% @type2 = @widgets.find_all(''type = 2'') %> > >(untested) @widgets.select { |w| w.type == 2 } @widgets is a Ruby Array, basically. -- Best regards, Alexey Verkhovsky Ruby Forum: http://ruby-forum.org (moderator) RForum: http://rforum.andreas-s.net (co-author) Instiki: http://instiki.org (maintainer)
Thanks Alexey. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Alexey Verkhovsky Sent: Thursday, 21 April 2005 2:14 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Can ActiveRecord do this? Neville Burnell wrote:><% @type1 = @widgets.find_all(''type = 1'') %> <% @type2 = >@widgets.find_all(''type = 2'') %> > >(untested) @widgets.select { |w| w.type == 2 } @widgets is a Ruby Array, basically. -- Best regards, Alexey Verkhovsky Ruby Forum: http://ruby-forum.org (moderator) RForum: http://rforum.andreas-s.net (co-author) Instiki: http://instiki.org (maintainer) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Thursday, April 21, 2005, 2:03:16 PM, Neville wrote:> Hi,> I''d like to be able to query a ''result set'' without hitting the db again > for example:> In my controller:> Def show > #load somel widgets > @widgets = Widgets.find_all(''type = 1 or type = 2'') > End> In my view:> <% @type1 = @widgets.find_all(''type = 1'') %> > <% @type2 = @widgets.find_all(''type = 2'') %>> I tried this in Rails 12.0 but additional db hits were created.Try using Ruby instead of SQL :) <% @type1 = @widgets.select { |w| w.type == 1 } %> BTW, don''t automatically use an instance variable (@type1) instead of a local variable (type1). A local variable is probably all you need in the view.