# Session scope?

**URL:** <https://www.ruby-forum.com/t/session-scope/66887>\
**Category:** Rails\
**Created:** [December 15, 2005, 8:40pm UTC](https://www.ruby-forum.com/t/session-scope/66887 "2005-12-15T20:40:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [December 15, 2005, 8:40pm UTC](https://www.ruby-forum.com/t/session-scope/66887/1 "2005-12-15T20:40:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [December 15, 2005, 9:01pm UTC](https://www.ruby-forum.com/t/session-scope/66887/2 "2005-12-15T21:01:56Z")

</div>

On Dec 15, 2005, at 11:38 AM, Harm de Laat wrote:

> 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 Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [December 15, 2005, 9:07pm UTC](https://www.ruby-forum.com/t/session-scope/66887/3 "2005-12-15T21:07:59Z")

</div>

Great!, Thanks this worked for me!

---

<div class="post-metadata">

**Author:** ![Harm\_de\_Laat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de053/32.png) [@Harm\_de\_Laat](https://www.ruby-forum.com/u/Harm_de_Laat)\
**Post date:** [December 16, 2005, 12:04am UTC](https://www.ruby-forum.com/t/session-scope/66887/4 "2005-12-16T00:04:37Z")

</div>

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.
