# Where to put this to have access from everywhere

**URL:** <https://www.ruby-forum.com/t/where-to-put-this-to-have-access-from-everywhere/118387>\
**Category:** Rails\
**Created:** [October 26, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/where-to-put-this-to-have-access-from-everywhere/118387 "2007-10-26T12:57:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [October 26, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/where-to-put-this-to-have-access-from-everywhere/118387/1 "2007-10-26T12:57:14Z")

</div>

I have this

class Pic \< ActiveRecord::Base  
def self.active\_pics  
find(:all, :conditions =\> [“thumbnail is NULL AND active = 1”])  
end  
end

in my pic.rb model file.

I have an area on my site which uses a different layout file  
(pic.html.erb). Within the layout file I render a special partial  
(\_pics\_nav.html.erb):

\<% for pic in @pics %\>  
\<%= pic.description %\>  
\<% end %\>

So I get a nil object.

Where tu put this code so I have access within every layout file and  
every partial?

Thanx

–  
Jochen K.

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [October 26, 2007, 1:01pm UTC](https://www.ruby-forum.com/t/where-to-put-this-to-have-access-from-everywhere/118387/2 "2007-10-26T13:01:57Z")

</div>

On 26 Oct 2007, at 11:56, Jochen K. wrote:

> Where tu put this code so I have access within every layout file and  
> every partial?

Don’t rely on the instance variable :  
\<%= render :partial =\> ‘/shared/pics\_nav’, :object =\> Pic.active\_pics %\>

If this is quite common, i’d stick something in application helper

def render\_pic\_nav\_bar  
render :partial =\> ‘/shared/pics\_nav’, :object =\> Pic.active\_pics %\>  
end

and then you just do \<%= render\_pic\_nav\_bar%\> when you need it.  
Fred

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [October 26, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/where-to-put-this-to-have-access-from-everywhere/118387/3 "2007-10-26T13:07:47Z")

</div>

Frederick C. schrieb:

> > So I get a nil object.  
> > def render\_pic\_nav\_bar  
> > render :partial =\> ‘/shared/pics\_nav’, :object =\> Pic.active\_pics %\>  
> > end
> 
> and then you just do \<%= render\_pic\_nav\_bar%\> when you need it.  
> Fred

Wow! That easy!

Thanx a lot!!

–  
Jochen K.
