Folks, I''m developing a blog plugin which adds a bunch of ''has_many'' associations to the user object, e.g.: has_many :blogs_created, :class_name => ''Blog'', :foreign_key => ''created_by'' The caveat is that I''m trying to piggy-back off of an existing User model. If possible, I''d like to have a module with the ''has_many'' commands, etc. in my plugin/lib folder, and then include said module into the existing User model. I''ve been able to include other methods this way, but get the following error message when I try to mixin ''has_many'': undefined method `has_many'' for BlogUser:Module Right now the code looks like this: class User < ActiveRecord::Base include BlogUser # Include plugin''s predefined has_many associations [already existing stuff] ... (in plugin folder/lib, accessible thanks to Engines) module BlogUser has_many :blogs_created, :class_name => ''Blog'', :foreign_key => ''created_by'' ... If you can help, I''d greatly appreciate it! Thanks! ~Mike -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Couldn''t you just extend the User class in your plugin? Define this in your plugin directory: class User < ActiveRecord::Base has_many :blogs end On Dec 27, 2007 2:37 PM, Mike Laurence <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Folks, > > I''m developing a blog plugin which adds a bunch of ''has_many'' > associations to the user object, e.g.: > > has_many :blogs_created, :class_name => ''Blog'', :foreign_key => > ''created_by'' > > The caveat is that I''m trying to piggy-back off of an existing User > model. If possible, I''d like to have a module with the ''has_many'' > commands, etc. in my plugin/lib folder, and then include said module > into the existing User model. I''ve been able to include other methods > this way, but get the following error message when I try to mixin > ''has_many'': > > undefined method `has_many'' for BlogUser:Module > > Right now the code looks like this: > > class User < ActiveRecord::Base > include BlogUser # Include plugin''s predefined has_many associations > > [already existing stuff] > ... > > (in plugin folder/lib, accessible thanks to Engines) > module BlogUser > has_many :blogs_created, :class_name => ''Blog'', :foreign_key => > ''created_by'' > ... > > If you can help, I''d greatly appreciate it! Thanks! > > ~Mike > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Yes, it is possible. Check out how to develop plugins screencasts at www. Rubyplus.org Sent from my iPhone On Dec 26, 2007, at 8:07 PM, Mike Laurence <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:> > Folks, > > I''m developing a blog plugin which adds a bunch of ''has_many'' > associations to the user object, e.g.: > > has_many :blogs_created, :class_name => ''Blog'', :foreign_key => > ''created_by'' > > The caveat is that I''m trying to piggy-back off of an existing User > model. If possible, I''d like to have a module with the ''has_many'' > commands, etc. in my plugin/lib folder, and then include said module > into the existing User model. I''ve been able to include other methods > this way, but get the following error message when I try to mixin > ''has_many'': > > undefined method `has_many'' for BlogUser:Module > > Right now the code looks like this: > > class User < ActiveRecord::Base > include BlogUser # Include plugin''s predefined has_many > associations > > [already existing stuff] > ... > > (in plugin folder/lib, accessible thanks to Engines) > module BlogUser > has_many :blogs_created, :class_name => ''Blog'', :foreign_key => > ''created_by'' > ... > > If you can help, I''d greatly appreciate it! Thanks! > > ~Mike > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
Here is some sample code from a plugin that I developed: http://sra.rubyforge.org/svn/plugins/simply_rich_association/lib/simply_rich_association.rb On Dec 26, 2007 8:21 PM, Ryan Bigg <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Couldn''t you just extend the User class in your plugin? > > Define this in your plugin directory: > > class User < ActiveRecord::Base > has_many :blogs > end > >-- http://www.rubyplus.org/ Free Ruby and Rails Screencasts --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 26, 2007, at 10:21 PM, Ryan Bigg wrote:> Couldn''t you just extend the User class in your plugin? > > Define this in your plugin directory: > > class User < ActiveRecord::Base > has_many :blogs > end >I''ve not done any plugin development, but wouldn''t that require that the application using the plugin *have* a User class? It seems more like you''d want to introspectively add the has_many to the "parent" thing of BlogUser. Hmm. Maybe I should go watch that plugin development screencast... :) Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 26, 2007 11:07 PM, Mike Laurence <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Folks, > > I''m developing a blog plugin which adds a bunch of ''has_many'' > associations to the user object, e.g.: > > has_many :blogs_created, :class_name => ''Blog'', :foreign_key => > ''created_by'' > > The caveat is that I''m trying to piggy-back off of an existing User > model. If possible, I''d like to have a module with the ''has_many'' > commands, etc. in my plugin/lib folder, and then include said module > into the existing User model. I''ve been able to include other methods > this way, but get the following error message when I try to mixin > ''has_many'': > > undefined method `has_many'' for BlogUser:Module > > Right now the code looks like this: > > class User < ActiveRecord::Base > include BlogUser # Include plugin''s predefined has_many associations > > [already existing stuff] > ... > > (in plugin folder/lib, accessible thanks to Engines) > module BlogUser > has_many :blogs_created, :class_name => ''Blog'', :foreign_key => > ''created_by'' > ...Has_many is a class method, you need to invoke it against the class which includes your module: module BlogUser def self.included(includer) includer.class_eval do has_many :blogs_created, :class_name => ''Blog'', :foreign_key => ''created_by'' end end end -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 27, 2007 10:56 PM, Phillip Koebbe <phillipkoebbe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Dec 26, 2007, at 10:21 PM, Ryan Bigg wrote: > > > Couldn''t you just extend the User class in your plugin? > > > > Define this in your plugin directory: > > > > class User < ActiveRecord::Base > > has_many :blogs > > end > > > > I''ve not done any plugin development, but wouldn''t that require that > the application using the plugin *have* a User class? It seems more > like you''d want to introspectively add the has_many to the "parent" > thing of BlogUser. > > Hmm. Maybe I should go watch that plugin development screencast... :) > > Peace, > Phillip > >Surprisingly not. If there is not a User class it''ll create a User class for you. If there is a User class, it''ll just extend it. That''s the brilliance of Ruby. -- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 27, 2007, at 5:49 PM, Ryan Bigg wrote:> > > On Dec 27, 2007 10:56 PM, Phillip Koebbe <phillipkoebbe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Dec 26, 2007, at 10:21 PM, Ryan Bigg wrote: > > > Couldn''t you just extend the User class in your plugin? > > > > Define this in your plugin directory: > > > > class User < ActiveRecord::Base > > has_many :blogs > > end > > > > I''ve not done any plugin development, but wouldn''t that require that > the application using the plugin *have* a User class? It seems more > like you''d want to introspectively add the has_many to the "parent" > thing of BlogUser. > > Hmm. Maybe I should go watch that plugin development screencast... :) > > Peace, > Phillip > > > > Surprisingly not. If there is not a User class it''ll create a User > class for you. If there is a User class, it''ll just extend it. > That''s the brilliance of Ruby.Hi Ryan, I seem to have overlooked > Define this in your plugin directory: I see what you''re getting at now. By putting this in the plugin, it will either extend the User class or create it (to basically repeat what you just wrote). Nice point. +1 Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Rick Denatale wrote:> On Dec 26, 2007 11:07 PM, Mike Laurence > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> model. If possible, I''d like to have a module with the ''has_many'' >> include BlogUser # Include plugin''s predefined has_many associations >> >> [already existing stuff] >> ... >> >> (in plugin folder/lib, accessible thanks to Engines) >> module BlogUser >> has_many :blogs_created, :class_name => ''Blog'', :foreign_key => >> ''created_by'' >> ... > > Has_many is a class method, you need to invoke it against the class > which includes your module: > > module BlogUser > def self.included(includer) > includer.class_eval do > has_many :blogs_created, :class_name => ''Blog'', > :foreign_key => ''created_by'' > end > end > end > > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/Very interesting. This would be most useful for plugins, because you could have the user define any already existing class to receive the class methods. Thanks to everyone for your suggestions! Mike -- 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?hl=en -~----------~----~----~----~------~----~------~--~---