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 | [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] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org