Hi --
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?
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