Models inheritance

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 :slight_smile:

none?

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

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

On 9/26/06, Adrian L. [email protected] 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/

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 :slight_smile:
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 | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs