Hi, I''m new to Ruby and Rails and I would be very grateful for some advice. I''ve got the following code. class Foo < ActiveRecord::Base include Versionable # Some methods to handle my versioned objects has_many :versions, :class_name => "FooVersion", :foreign_key => "parent_id" belongs_to :curr, :class_name => "FooVersion", :foreign_key => "current_id" belongs_to :unapproved, :class_name => "FooVersion", :foreign_key => "unapproved_id" end I''d like to make use of this again with my Bar and BarVersion class without copying and pasting. Any thoughs on how I could do this? Thanks, Richard -- 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 Oct 22, 2006, at 2:19 PM, Rich H wrote:> I''d like to make use of this again with my Bar and BarVersion class > without copying and pasting. Any thoughs on how I could do this?In your Versionable module: def self.included(klass) versioned_class = klass.name + ''Version'' klass.send(:has_many, :versions, :class_name => versioned_class, :foreign_key =>"parent_id") klass.send(:belongs_to :curr, :class_name => versioned_class, :foreign_key => "current_id") klass.send(:belongs_to :unapproved, :class_name => versioned_class, :foreign_key => "unapproved_id") end Make sense? http://ruby-doc.org/core/classes/Module.html#M000743 -- Chris Wanstrath http://errtheblog.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 Oct 22, 2006, at 3:19 PM, Chris Wanstrath wrote:> On Oct 22, 2006, at 2:19 PM, Rich H wrote: > >> I''d like to make use of this again with my Bar and BarVersion class >> without copying and pasting. Any thoughs on how I could do this? > > In your Versionable module:But don''t dare forget the commas, like I did. The correct version: def self.included(klass) versioned_class = klass.name + ''Version'' klass.send(:has_many, :versions, :class_name => versioned_class, :foreign_key =>"parent_id") klass.send(:belongs_to, :curr, :class_name => versioned_class, :foreign_key => "current_id") klass.send(:belongs_to, :unapproved, :class_name => versioned_class, :foreign_key => "unapproved_id") end -- Chris Wanstrath http://errtheblog.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 Chris. I''ve done something similar and added it to a plugin. It works perfectly. Thanks again. -- 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 -~----------~----~----~----~------~----~------~--~---