# Where does logic for the layout go?

**URL:** <https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776>\
**Category:** Rails\
**Created:** [April 25, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776 "2006-04-25T20:32:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 25, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776/1 "2006-04-25T20:32:55Z")

</div>

on the main layout for my controller, i need a list of users - each with  
their own link to a lists of tasks that they have been assigned.

i did have a line inside the .rhtml file that found all the users, but  
i’m guessing this is probably the wrong place to put them. i’m new to  
ruby and rails, so i still struggle a little with the MVC concept.

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 25, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776/2 "2006-04-25T20:52:50Z")

</div>

I would think that putting the find command in your model would be the  
best place.

On Tue, 2006-04-25 at 20:32 +0200, Josh K. wrote:

> on the main layout for my controller, i need a list of users - each with  
> their own link to a lists of tasks that they have been assigned.
> 
> i did have a line inside the .rhtml file that found all the users, but  
> i’m guessing this is probably the wrong place to put them. i’m new to  
> ruby and rails, so i still struggle a little with the MVC concept.

Charlie B.  
[www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 25, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776/3 "2006-04-25T21:06:11Z")

</div>

Charlie B. wrote:

> I would think that putting the find command in your model would be the  
> best place.

when putting something in the model, does it have to be within a “def  
action\_name” block, or can it be defined anywhere?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 25, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776/4 "2006-04-25T21:23:50Z")

</div>

Yes, I would create a method in the model to find the list of users. I  
would then call the method from within my controller. Here’s a little  
sudo code.

Model:  
def find\_users

## put your sql or find statement here

end

Controller  
@users = User.find\_users

Charlie bowman  
[www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 25, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776/5 "2006-04-25T21:33:16Z")

</div>

On Apr 25, 2006, at 03:20 PM, Charlie B. wrote:

> @users = User.find\_users  
> Quick correction here: if you want to use the method in this manner,  
> where you don’t have an existing instance of the User class that you  
> are performing the method on, you have to define the method as a  
> class method, not an instance method, like so:

def self.find\_users  
…  
end

Btw, this is exactly what I recommend doing, especially in those  
cases where the kind of find you want to perform is one you will do  
in more than one location and the complexity of the find is more than  
you can do with a simple dynamic finder:  
User.find\_all\_by\_firstname\_and\_lastname(params[:firstname], params  
[:lastname])

-Brian

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 25, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/where-does-logic-for-the-layout-go/58776/6 "2006-04-25T21:48:50Z")

</div>

so for

> > @users = User.find\_users  
> > if it is not inside a “def” block of code, does that make it accessible  
> > to all of the actions in that controller?
