# using a controller method in model

**URL:** <https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136>\
**Category:** rubyonrails-talk\
**Created:** [February 7, 2009, 3:10pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136 "2009-02-07T15:10:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmh1](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@fmh1](https://discuss.rubyonrails.org/u/fmh1)\
**Post date:** [February 7, 2009, 3:10pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/1 "2009-02-07T15:10:33Z")

</div>

hello,

how to use a controller method in model?

example

---- in -ApplicationController

def is\_ok? &nbsp;&nbsp;return true unless ........ end

----- in model

use is\_ok?

---

<div class="post-metadata">

**Author:** ![CFC](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@CFC](https://discuss.rubyonrails.org/u/CFC)\
**Post date:** [February 7, 2009, 3:12pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/2 "2009-02-07T15:12:28Z")

</div>

why you need to do this?

---

<div class="post-metadata">

**Author:** ![fmh1](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@fmh1](https://discuss.rubyonrails.org/u/fmh1)\
**Post date:** [February 7, 2009, 3:21pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/3 "2009-02-07T15:21:06Z")

</div>

because i have a special controller authentication (an admin model without table) and in my model i have a method to authorize some action.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 7, 2009, 3:35pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/4 "2009-02-07T15:35:53Z")

</div>

fmh wrote:

> because i have a special controller authentication (an admin model without table) and in my model i have a method to authorize some action.

You are trying to break the MVC pattern, only bad things will happen to you, you certainly have to refactor / rethink your code.

---

<div class="post-metadata">

**Author:** ![fmh1](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@fmh1](https://discuss.rubyonrails.org/u/fmh1)\
**Post date:** [February 7, 2009, 3:50pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/5 "2009-02-07T15:50:31Z")

</div>

i did this , because i dont want integrate my users admin with simple users

class Admin \< ActiveRecord::BaseWithoutTable &nbsp;&nbsp;column :username , :string &nbsp;&nbsp;column :userpass , :string

&nbsp;&nbsp;def self.authenticate?(username,userpass) &nbsp;&nbsp;&nbsp;&nbsp;admin = Admin.new &nbsp;&nbsp;&nbsp;&nbsp;admin.username = "admin" &nbsp;&nbsp;&nbsp;&nbsp;admin.userpass = "1234" &nbsp;&nbsp;&nbsp;&nbsp;if (admin.username == username && admin.userpass == userpass) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;end end

how to affect this admin to a current\_user ?

because if my current\_user is an admin user i skip my other problem

---

<div class="post-metadata">

**Author:** ![Rick3](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@Rick3](https://discuss.rubyonrails.org/u/Rick3)\
**Post date:** [February 7, 2009, 9:27pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/6 "2009-02-07T21:27:38Z")

</div>

Ok,

So once we get past the "Why would you ever want to do something dumb like that?" and "Bad things will happen..." which I assume means the Rails Police will come during the night and kill your cat, how can FMH actually get the job done?

Seriously guys, give the guy at least a pointer to a real reason not to do what he's trying - don't just spit dogma.

This has worked for me with: About your application's environment Ruby version 1.8.7 (powerpc-darwin9) RubyGems version 1.3.1 Rails version 2.2.2 Active Record version 2.2.2 Action Pack version 2.2.2 Active Resource version 2.2.2 Action Mailer version 2.2.2 Active Support version 2.2.2 Application root /Users/rick/Journeys Environment development Database adapter postgresql

1) put your needed methods in a module in "lib/your\_stuff.rb" where your\_stuff.rb looks like:

module YourStuff &nbsp;&nbsp;protected &nbsp;&nbsp;&nbsp;&nbsp;def thing1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;end end

2) add the line "include YourStuff" to the controller for the class you want to make thing1 available in:

class PagesController \< ApplicationController &nbsp;&nbsp;include AuthenticatedSystem

or, optionally, add it to app/controllers/application.rb to make thing1 available everywhere.

Now let's see if there can be some reasonable discussion about why this is a "bad idea" and how soon you should expect your cat to die.

Rick

---

<div class="post-metadata">

**Author:** ![fmh1](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@fmh1](https://discuss.rubyonrails.org/u/fmh1)\
**Post date:** [February 8, 2009, 2:08am UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/7 "2009-02-08T02:08:05Z")

</div>

ok, i think i rushed coding this, i now rethinking my code. i am not familiar with MVC.

---

<div class="post-metadata">

**Author:** ![Rick3](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@Rick3](https://discuss.rubyonrails.org/u/Rick3)\
**Post date:** [February 8, 2009, 5:52am UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/8 "2009-02-08T05:52:03Z")

</div>

Look here for some examples on how to do role based authorization.

[http://www.vaporbase.com/postings/Authorization\_in\_Rails](http://www.vaporbase.com/postings/Authorization_in_Rails)

You can also checkout restful-authentication to support the user login type of accounts.

[http://github.com/technoweenie/restful-authentication/tree/master](http://github.com/technoweenie/restful-authentication/tree/master)

These two work well together.

---

<div class="post-metadata">

**Author:** ![fmh1](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@fmh1](https://discuss.rubyonrails.org/u/fmh1)\
**Post date:** [February 8, 2009, 12:04pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/9 "2009-02-08T12:04:13Z")

</div>

yes i have a user table, now I have included my admins in, with a column UserType and it works very well with restfulauthentication. thanks

---

<div class="post-metadata">

**Author:** ![Julian\_Leviston](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Julian\_Leviston](https://discuss.rubyonrails.org/u/Julian_Leviston)\
**Post date:** [February 8, 2009, 12:27pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/10 "2009-02-08T12:27:58Z")

</div>

Sometimes we need code accessible from everywhere. Usually models  
relate to structure of data for storage and business logic.  
Controllers are for the gluing of interface to model and views are  
interface related. Ask questions if you get stuck

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/) Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![Jean-Marc\_M2i3.com](https://avatars.discourse-cdn.com/v4/letter/j/858c86/32.png) [@Jean-Marc\_M2i3.com](https://discuss.rubyonrails.org/u/Jean-Marc_M2i3.com)\
**Post date:** [February 24, 2009, 2:18pm UTC](https://discuss.rubyonrails.org/t/using-a-controller-method-in-model/39136/11 "2009-02-24T14:18:08Z")

</div>

Hi FMH,

seems you are looking to have a "special case" of a user (a user who is an admin) .... look at this pattern from Martin Fowler and see if it applies... [P of EAA: Special Case](http://martinfowler.com/eaaCatalog/specialCase.html)

I got to say that within MVC, the Controller should not interfere with the Model as the responsibility of the controller is to select with model and view to use and what to do with the model (not publish anything inside the model).

In short MVC can be viewed as split responsibilities (definitions for a lot of these patterns is there [Catalog of Patterns of Enterprise Application Architecture](http://martinfowler.com/eaaCatalog/) but frankly go get the book, you'll do yourself a favor): &nbsp;&nbsp;&nbsp;(M)odel: the business logic, data storage &nbsp;&nbsp;&nbsp;(C)ontroller:deciding which model and view are best to handle the user request &nbsp;&nbsp;&nbsp;(V): rendering the model based on decision made by the controller

BTW, not tying a model to a table is perfectly acceptable you know.

Jean-Marc [http://m2i3.com](http://m2i3.com)
