2012-01-31 51 views
0

我有一堆activerecord類,看起來像我喜歡的類似!如何DRY多態activerecord類?

我可以擴展一個基類來幹這些方法嗎,還是會混淆rails?

也許我可以分享一些東西,但不是別人?

如果不是 - 最好的方法是什麼?

千恩萬謝;)

class Stage < ActiveRecord::Base 
    acts_as_taggable 
    has_many :services, :as => :serviceable 
    belongs_to :event 
    belongs_to :user 

    after_save :tag! 

    def t(s) 
    self.tag_list.add s 
    self.event.tag_list.add s 
    end 

    # injected to after_save -> http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html 
    def tag! 
    s = self 

     if s.id > 0 then s.t "id-greater-than-0" end 
     if s.id > 0 then s.t "some-stage-specific-stuff" end 

    self.tag_list  
    end 
end 



class Sound < ActiveRecord::Base 
    acts_as_taggable 
    has_many :services, :as => :serviceable 
    belongs_to :event 
    belongs_to :user 

    after_save :tag! 

    def t(s) 
    self.tag_list.add s 
    self.event.tag_list.add s 
    end 

    # injected to after_save -> http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html 
    def tag! 
    s = self 

     if s.id > 0 then s.t "some-sound-specific-stuff" end 

    self.tag_list  
    end 
end 

回答