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 :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
none? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Adrian Liem 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Liem 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 9/26/06, Adrian Liem <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 advanceTake a look at some good plugins. These will show you how to do these things. Technoweenies (Rick Olson) 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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Sep-26 11:34 UTC
Re: Models inheritance
Hi -- On Tue, 26 Sep 2006, Adrian Liem wrote:> > 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 -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---