문제

나는 기존 문제에 문제가 있습니다 ActiveRecord::Observer 다양한 레코딩 모델 Activities a User 사이트. 내가 시도 할 때까지 모든 것이 정말 잘 작동했습니다. observe 그만큼 User 동일 한 클래스 Activity 사용하는 모델 observe 다른 모델. 고려하십시오 :

class Activity < ActiveRecord::Base
    belongs_to :user
    belongs_to :item, :polymorphic => true
end

class ActivityObserver < ActiveRecord::Observer
    observe :billing, :call, :vanity_number
end

class User < ActiveRecord::Base
    has_many :comments
    has_many :activities
end

class Comment < ActiveRecord::Base
    belongs_to :user
    has_many :activities, :as => :item
end

위는 잘 작동했습니다. 쿼리 User.activities 행을 반환합니다 Comment Activities. 내가 추가하자마자 :user ~로 observe 방법 ActivityObserver 그리고 변경되었습니다 has_many :activities 에서 User 모델 has_many :activities, :as => :item, User.activities 그와 관련된 활동이었던 행만 반환 할 것입니다. User 인스턴스 및 ~ 아니다 어느 Comments

왜 이런거야? 이 작업을 예상대로 작동 시키려면 어떻게해야합니까?

도움이 되었습니까?

해결책

변경했을 때

class User < ActiveRecord::Base
  has_many :activities
end

에게

class User < ActiveRecord::Base
  has_many :activities, :as => :item
end

지금은보고 있습니다 item_id 유형 필드가 사용자 인 활동 테이블의 필드에서 item_id = users.id. 그것이보기 전에 user_id 활동 분야. 이후 item_id 필드 및 유형 필드에는 두 가지 값이 없으므로 사용자 또는 댓글의 활동을 가리킬 수 있도록했습니다. 둘 다 될 수 없습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top