2012-01-04 2 views
0

각 모델마다 속성 모델과의 다형성 연관을 공유하는 다양한 모델이 있습니다. 내가 믹스 인을 조금 쓰려고 코드를 작성하려하지만, 작동하지 않습니다. 제발 디버깅 도움을 제공해주세요. 내 믹스 인은 '정의 (나는이 모든 것을 실행하면,하지만 협회가 보인다Rails 3.1에서 모델 연결을 정의하는 Mixin

class Myobjects < ActiveRecord::Base 
    include ModelWithProperties 
end 

module ModelWithProperties 
    def self.included?(base) 
     base.class_eval do 
      has_many :properties, :as=>:parent 
     end 
    end 

    def examplesharedfunction 
     /// stuff here 
    end 
end 

그리고 내 모델은 다음과 같이

이 ... 촬영 한 것으로 ... 다음과 같습니다 #can 액세스 방법 'properies'는 examplesharedfunction.

모든 단서/팁?

답변

4

모듈이 포함 된 경우 호출되는 후크가 self.included하지입니다 그게 다 -

또한 ActiveSupport::Concern

module M 
    extend ActiveSupport::Concern 

    included do 
    has_many :properties, :as=>:parent 
    end 
end 
+0

브릴리언트 사용할 수 있습니다. 고마워. – user1129657

관련 문제