Is it possible to override has_many''s :order attribute at run-time? e.g. something like: <% for category in @site.categories(:order=>''name'') %> ... <% for category in @site.categories(:order=>''rank'') %> ... thanks csn -- Posted via http://www.ruby-forum.com/.
csn wrote:> Is it possible to override has_many''s :order attribute at run-time? e.g. > something like: > > <% for category in @site.categories(:order=>''name'') %> > ... > > <% for category in @site.categories(:order=>''rank'') %> > ... > > > thanks > csn<% for category in @site.categories.find(:all, :order=>''name'') %> Gokhan Arli Sylow web development :: www.sylow.net -- Posted via http://www.ruby-forum.com/.
Does ActiveRecord only load the data once and keep it cached internally? ie: would this only hit the db once? <% for category in @site.categories.find(:all, :order=>''name'') %> <% for category in @site.categories.find(:all, :order=>''rank'') %>> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Gokhan Arli > Sent: Wednesday, January 25, 2006 4:50 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] Re: Override has_many :order at run-time? > > csn wrote: > > Is it possible to override has_many''s :order attribute at run-time? e.g. > > something like: > > > > <% for category in @site.categories(:order=>''name'') %> > > ... > > > > <% for category in @site.categories(:order=>''rank'') %> > > ... > > > > > > thanks > > csn > > <% for category in @site.categories.find(:all, :order=>''name'') %> > > Gokhan Arli > Sylow web development :: www.sylow.net > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Jan 25, 2006, at 6:04 PM, Bob Silva wrote:> Does ActiveRecord only load the data once and keep it cached > internally? > > ie: would this only hit the db once? > > <% for category in @site.categories.find(:all, :order=>''name'') %> > > <% for category in @site.categories.find(:all, :order=>''rank'') %>Check the development log, and you shall have the answer directly. I''m quite sure the answer is no. You could put this loop into a partial, render it to a string in the controller, and reference the string in the view twice. -- -- Tom Mornini
On Jan 25, 2006, at 6:29 PM, Tom Mornini wrote:> On Jan 25, 2006, at 6:04 PM, Bob Silva wrote: > >> Does ActiveRecord only load the data once and keep it cached >> internally? >> >> ie: would this only hit the db once? >> >> <% for category in @site.categories.find(:all, :order=>''name'') %> >> >> <% for category in @site.categories.find(:all, :order=>''rank'') %> > > Check the development log, and you shall have the answer directly. > > I''m quite sure the answer is no. > > You could put this loop into a partial, render it to a string in > the controller, and reference the string in the view twice.Oops, did not notice the orders were different! So...make that "The answer is no." :-) You could grab the categories in the controller, then use a Ruby sort in the view... controller: @categories = @site.categories view: <% for category in @categories.sort { |a,b| a.rank <=> b.name } %> <% for category in @categories.sort { |a,b| a.rank <=> b.rank } %> -- -- Tom Mornini