2011-08-25 19 views
2

只是希望有人可以解釋這對我來說..的Rails/Ruby的密新帶來不必要的其他類中的方法

我有進口的模塊站點類:

網站

class Site < ActiveRecord::Base 
include TrackableChanges 
... 

in trackable_changes.rb

我有此代碼..

module TrackableChanges 
    include ActiveSupport::Callbacks 

    def self.included(base) 
     # Initialize module. 
     base.has_many :change_requests, :as => :model, :dependent => :destroy 
     #Callbacks 
     base.before_save :before_save_change_request 
     base.after_save :after_save_change_request 
     base.before_destroy :before_destroy_change_request 

     Facility 

    end 
... 

參考設施真的讓我感到困惑(我在這裏放了一個簡單的參考..)。基本上在Facility.rb我有這個..

class Facility < ActiveRecord::Base 
    acts_as_citier 

acts_as_citier看起來有點像這樣:

module Citier 
    def self.included(base) 
    # When a class includes a module the module’s self.included method will be invoked. 
    base.send :extend, ClassMethods 
    end 
end 

ActiveRecord::Base.send :include, Citier 

現在..只要按我最初的模塊中引用設施它要在acts_as_citier寶石和延伸我的網站類的ActiveRecord。我希望act_as_citier gem適合我的設施,但不適合我的網站。

任何人都可以幫助阻止這包括引入這個不必要的參考!

編輯

好像我不能引用類的設施都沒有它帶來了在該基金定義通過它的參考寶石act_as_citier

城市做的ActiveRecord類添加劑這...

ActiveRecord::Base.send :include, Citier 
+0

請[不要在您的帖子中使用簽名或標語](http://stackoverflow.com/faq#signatures)。 – meagar

回答

0
class Facility < ActiveRecord::Base 
    #attr_accessible :name, :type, :facility_type_id, :accessibility_id, :tldc_id, :site_id, :tldc_approved 

    acts_as_citier if self.to_s == 'Facility' #Protects other classes from accidently getting the AR additions 

添加條件到包括停止acts_as_citier gem擴展任何引用'Facility'的類。

我假設包含在我的網站class>貫穿模塊>當它碰到對Facility class的引用時>貫穿Facility.rb>它通過acts_as_citier運行,然後命中extend到活動記錄行。它幫助我記住.rb文件的每個部分都是可執行文件。