# Models inheritance

**URL:** <https://www.ruby-forum.com/t/models-inheritance/69711>\
**Category:** Rails\
**Created:** [September 23, 2006, 3:31am UTC](https://www.ruby-forum.com/t/models-inheritance/69711 "2006-09-23T03:31:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [September 23, 2006, 3:31am UTC](https://www.ruby-forum.com/t/models-inheritance/69711/1 "2006-09-23T03:31:20Z")

</div>

hello i’m a newbie, i have these models:

class Document \< AR  
def self.authorized?  
def self.set\_current\_user\_id(user\_id)

class Blog \< AR  
def self.authorized?  
def self.set\_current\_user\_id(user\_id)

class Gallery \< AR  
def self.authorized?  
def self.set\_current\_user\_id(user\_id)

and i want to inherit these models from one class/model – since this  
violates the DRY rule. especially the set\_current\_user\_id is kinda  
tiring, since i have to call it first in every controller action before  
i use the authorized? method.

i’ve search and read the forum, but i don’t find enough explanation to  
get through this. thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [September 24, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/models-inheritance/69711/2 "2006-09-24T17:08:58Z")

</div>

none?

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [September 24, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/models-inheritance/69711/3 "2006-09-24T17:34:40Z")

</div>

Adrian L. wrote:

> and i want to inherit these models from one class/model – since this  
> violates the DRY rule. especially the set\_current\_user\_id is kinda  
> tiring, since i have to call it first in every controller action before  
> i use the authorized? method.

You can use a mix-in to provide that functionality. Write a module,  
stick it in lib, and then include in each model that requires it.

- Roderick

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [September 26, 2006, 2:52am UTC](https://www.ruby-forum.com/t/models-inheritance/69711/4 "2006-09-26T02:52:53Z")

</div>

thanks  
i’ve read AWDWR and ruby for rails book, and searched from the forum,  
but i still haven’t quite understand about this subject, could someone  
please help me, or at least give me the reference about this subject.

i have successfully made the model instance method from module:

lib:  
module ModelBelongsToUser  
def model\_instance\_method  
“value from module”  
end  
end

model:  
class Blog  
include ModelBelongsToUser  
end

controller:  
class BlogController  
def index  
blog = Blog.new  
@value = blog.model\_instance\_method  
end  
def index2  
@value = Blog.model\_class\_method  
end  
end

but how should i make the model\_class\_method in the module?  
and how should i make the model class variable in the module? is it  
possible?  
(i want to put the current\_user variable in the model class variable,  
because it seems the model cannot access the controller variables)

thanks in advance

Roderick van Domburg wrote:

> Adrian L. wrote:
> 
> > and i want to inherit these models from one class/model – since this  
> > violates the DRY rule. especially the set\_current\_user\_id is kinda  
> > tiring, since i have to call it first in every controller action before  
> > i use the authorized? method.
> 
> You can use a mix-in to provide that functionality. Write a module,  
> stick it in lib, and then include in each model that requires it.
> 
> - Roderick

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [September 26, 2006, 3:28am UTC](https://www.ruby-forum.com/t/models-inheritance/69711/5 "2006-09-26T03:28:19Z")

</div>

On 9/26/06, Adrian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> module ModelBelongsToUser  
> controller:  
> but how should i make the model\_class\_method in the module?  
> and how should i make the model class variable in the module? is it  
> possible?  
> (i want to put the current\_user variable in the model class variable,  
> because it seems the model cannot access the controller variables)
> 
> thanks in advance

Take a look at some good plugins. These will show you how to do these  
things.

Technoweenies (Rick O.) plugins are very good, are self documenting  
and  
also have reasonably good documentation in the source.

Check out the acts\_as\_\* plugins inparticular. These are chocked full of  
moduly goodness.

[http://svn.techno-weenie.net/projects/plugins/](http://svn.techno-weenie.net/projects/plugins/)

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [September 26, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/models-inheritance/69711/6 "2006-09-26T13:38:38Z")

</div>

Hi –

On Tue, 26 Sep 2006, Adrian L. wrote:

> def model\_instance\_method  
> class BlogController  
> def index  
> blog = Blog.new  
> @value = blog.model\_instance\_method  
> end  
> def index2  
> @value = Blog.model\_class\_method  
> end  
> end
> 
> but how should i make the model\_class\_method in the module?

Keep in mind that the class Blog is an object in its own right, and  
has a different method lookup path than _instances_ of Blog.

If you want to add functionality to Blog itself, with a module, you  
can put the methods you want in a module, and then extend Blog:

module ModelBelongsToUser  
module ModelClassDoesSomething # or whatever  
def model\_class\_method  
end  
end  
end

class Blog \< AR::Base  
extend ModelBelongsToUser::ModelClassDoesSomething  
…  
end

or something like that. extend is a way of adding module  
functionality to one particular object, in this case the class object  
Blog. (Of course you only need to take the module approach to any of  
this if you’re planning to use the code in more than one place.)

> and how should i make the model class variable in the module? is it  
> possible?  
> (i want to put the current\_user variable in the model class variable,  
> because it seems the model cannot access the controller variables)

I’m going to leave that one alone until I’ve had more coffee 🙂  
Except to say: class variables are generally more trouble than they’re  
worth. Also, the separation between model and controller is good; it  
forces you to keep your code reasonably logical. It’s probably not a  
good idea to try to circumvent the restrictions too much. The User  
model should not need the concept of a “current” user; rather, _any_  
user object, including the current user, should be designed so that  
you can ask it to do whatever it needs to.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
