# What goes where?/Scope question

**URL:** <https://www.ruby-forum.com/t/what-goes-where-scope-question/122763>\
**Category:** Rails\
**Created:** [December 11, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/what-goes-where-scope-question/122763 "2007-12-11T22:18:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 11, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/what-goes-where-scope-question/122763/1 "2007-12-11T22:18:36Z")

</div>

In a couple of parts …

* * *

In a controller:

The controller can use anything defined in  
app/controllers/application\_controller.rb

What else can it use?

- app/helpers/application\_helper.rb because if the above?
- app/helpers/\_helper.rb ?
- any other helpers

What code in models can it use?

* * *

In view, what is the scope of the variables?  
Do they have to be defined in the relevant controller?

What about the main template?  
What about “other” partials?

What methods can it use?  
Just the ones for the the controller?  
What about methods for the models?  
What about things defined in the libraries?

–  
I wonder why. I wonder why.  
I wonder why I wonder.  
I wonder why I wonder why  
I wonder why I wonder!  
– Richard Feynman

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 12, 2007, 12:09am UTC](https://www.ruby-forum.com/t/what-goes-where-scope-question/122763/2 "2007-12-12T00:09:33Z")

</div>

On 11 Dec 2007, at 21:07, Anton J Aylward wrote:

> In a couple of parts …
> 
> * * *
> 
> In a controller:
> 
> The controller can use anything defined in  
> app/controllers/application\_controller.rb

All your controllers inherit from ApplicationController, so any  
instance methods there are available from any instance of your  
controllers

> What else can it use?
> 
> - app/helpers/application\_helper.rb because if the above?
> - app/helpers/\_helper.rb ?
> - any other helpers

Helper methods are (by default) only available in the appropriate view

> What code in models can it use?  
> I think this question belies a misunderstanding of what is going on.  
> You can from a controller call any of the class methods for a model  
> (eg find), and if you have an instance of a model then you can call  
> any of its public methods. This isn’t a rails question though - it’s  
> just the way the language is.

> * * *
> 
> In view, what is the scope of the variables?  
> Do they have to be defined in the relevant controller?  
> Instance variables are copied accross. That’s all

> What about the main template?  
> What about “other” partials?
> 
> What methods can it use?  
> Helper methods (by default application\_helper and the helper  
> associated with the controller, but you can specify extra modules/  
> functions)

Fred

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 12, 2007, 2:12am UTC](https://www.ruby-forum.com/t/what-goes-where-scope-question/122763/3 "2007-12-12T02:12:54Z")

</div>

Frederick C. said the following on 11/12/07 04:23 PM:

> > 
> 
> All your controllers inherit from ApplicationController, so any  
> instance methods there are available from any instance of your  
> controllers

So in one controller I can’t access code from another controller?  
I can’t even write

a = OtherController.new

to get to them.

> > What else can it use?
> > 
> > - app/helpers/application\_helper.rb because if the above?
> > - app/helpers/\_helper.rb ?
> > - any other helpers
> 
> Helper methods are (by default) only available in the appropriate view

So I can’t access the helper methods for a controller?  
I seem to have helpers for each controller.  
Can I only access code in them from the view corresponding to that  
controller and not the controller?

> > What code in models can it use?

> I think this question belies a misunderstanding of what is going on.

🙂 Trying to be comprehensive 🙂

> You can from a controller call any of the class methods for a model  
> (eg find), and if you have an instance of a model then you can call  
> any of its public methods. This isn’t a rails question though - it’s  
> just the way the language is.

> > * * *
> > 
> > In view, what is the scope of the variables?  
> > Do they have to be defined in the relevant controller?

The examples I look at, for example instiki, seem to have an awful lot  
defined in the controller.

> Instance variables are copied accross. That’s all
> 
> > What about the main template?  
> > What about “other” partials?
> > 
> > What methods can it use?

> Helper methods (by default application\_helper and the helper  
> associated with the controller, but you can specify extra modules/  
> functions)

I presume you mean libraries.  
I understand ‘include’, but does that mean stuff in the views get access  
to all code that’s on the PATH?

I ask this because a view, perhaps the template, may be accessing things  
that don’t seem to appear in the controller. Am I seeing a side effect  
of the search PATH?

I realise that there shouldn’t be code in the view, but decisions about  
what to display seem OK, like this from instiki

 \<% if @page and (@page.name == 'HomePage') and (%w( show published print ).include?(@action\_name)) %\> \<%= h @web.name %\> \<% elsif @web %\> \<%= @title %\> in \<%= h @web.name %\> \<% else %\> \<%= @title %\> \<% end %\> \<%= @show\_diff ? ' (changes)' : '' %\> 

> Fred
> 
> > Just the ones for the the controller?  
> > What about methods for the models?  
> > What about things defined in the libraries?

> 

–  
Few problems cannot be solved by proper application of high explosives.

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 12, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/what-goes-where-scope-question/122763/4 "2007-12-12T21:37:59Z")

</div>

Frederick C. said the following on 12/12/07 09:41 AM:

> reference an undefined constant it will try and load the appropriate  
> file (so referencing Customer loads customer.rb automatically).

So putting something like this (and maybe more lines in it) in  
config/environment.rb is of no use, then?

config.load\_paths += %w(  
app/controllers  
app/models  
app/helpers  
lib  
app/controllers/admin  
app/helpers/admin  
).map { |path| File.join(RAILS\_ROOT, path) }

–  
Things are more like they are now than they ever were before.  
Dwight D. Eisenhower

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 13, 2007, 12:39am UTC](https://www.ruby-forum.com/t/what-goes-where-scope-question/122763/5 "2007-12-13T00:39:01Z")

</div>

On Dec 12, 8:37 pm, Anton J Aylward [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> app/helpers/admin
> ).map { |path| File.join(RAILS_ROOT, path) }
> 
> ```

Those are already all on the load path. Like I said the load path only  
influences whether when you say Customer whether rails will be able to  
track down customer.rb. That’s all it does.

Fred

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 12, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/what-goes-where-scope-question/122763/6 "2007-12-12T15:50:27Z")

</div>

On 11 Dec 2007, at 22:11, Anton J Aylward wrote:

> So in one controller I can’t access code from another controller?  
> I can’t even write
> 
> a = OtherController.new
> 
> to get to them.  
> Well assuming that the constructor for a controller takes no  
> arguments, you could then do a.something. It’s pretty much guaranteed  
> to not be what you really want.
> 
> > associated with the controller, but you can specify extra modules/  
> > effect  
> > of the search PATH?

I think you have a deep misunderstanding of what’s going on. There is  
no search path for ‘code’. Rails performs some magic so that if you  
reference an undefined constant it will try and load the appropriate  
file (so referencing Customer loads customer.rb automatically).

Fred
