Hi all, I''m working on a Rails application. In this application I retrieve a list of ''brands'' using a method somewhere defined in my controller: private def get_brands @brands = Product.find_by_sql("select distinct brand from products"); end However, I display this information in a listbox in the template for my controller. This means @brands should be available on each request. In Java I would accomplish this by putting the @brands reference on the session scope. What is an effective way to do this in Ruby? Regards, Harm _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Dec 15, 2005, at 11:38 AM, Harm de Laat wrote:> Hi all, > > I''m working on a Rails application. In this application I retrieve > a list of ''brands'' using a method somewhere defined in my controller: > > private > def get_brands > @brands = Product.find_by_sql("select distinct brand from > products"); > end > > However, I display this information in a listbox in the template > for my controller. > This means @brands should be available on each request. > > In Java I would accomplish this by putting the @brands reference on > the session scope. > What is an effective way to do this in Ruby? > > Regards, > > Harm >Harm- The easiest way is to use a before filter. If you need @brands to be set in every controller in your app then put the follwing in application.rb, but if you only need it in one controller then do it in there: class ApplicationController < ActionController::Base attr_reader :brands before_filter :get_brands private def get_brands @brands = Product.find_by_sql("select distinct brand from products"); end end That will make @brands available in any controller in your app. If you only need it in one just put the same code in that particular controller. Cheers- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Great!, Thanks this worked for me! On 12/15/05, Ezra Zygmuntowicz <ezra-SD1UcFUIF7QjH4SCZfkgnw@public.gmane.org> wrote:> > > On Dec 15, 2005, at 11:38 AM, Harm de Laat wrote: > > > Hi all, > > > > I''m working on a Rails application. In this application I retrieve > > a list of ''brands'' using a method somewhere defined in my controller: > > > > private > > def get_brands > > @brands = Product.find_by_sql("select distinct brand from > > products"); > > end > > > > However, I display this information in a listbox in the template > > for my controller. > > This means @brands should be available on each request. > > > > In Java I would accomplish this by putting the @brands reference on > > the session scope. > > What is an effective way to do this in Ruby? > > > > Regards, > > > > Harm > > > > Harm- > > The easiest way is to use a before filter. If you need @brands to > be > set in every controller in your app then put the follwing in > application.rb, but if you only need it in one controller then do it > in there: > > class ApplicationController < ActionController::Base > > attr_reader :brands > > before_filter :get_brands > > private > def get_brands > @brands = Product.find_by_sql("select distinct brand from > products"); > end > > end > > That will make @brands available in any controller in your app. If > you only need it in one just put the same code in that particular > controller. > > Cheers- > > -Ezra Zygmuntowicz > Yakima Herald-Republic > WebMaster > http://yakimaherald.com > 509-577-7732 > ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
If you do ||= instead of =, it won''t re-run the query if the @brands instance variable has already been set. If your brands list doesn''t change much, it could save you some duplicate requests. On 12/15/05, Harm de Laat <harmdelaat-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Great!, Thanks this worked for me! > > On 12/15/05, Ezra Zygmuntowicz <ezra-SD1UcFUIF7QjH4SCZfkgnw@public.gmane.org> wrote: > > > > > > before_filter :get_brands > > > > private > > def get_brands > > @brands = Product.find_by_sql("select distinct brand from > > products"); > > end