2013-04-18 4 views
0

나는 다음과 같은 모델을 가지고 : 당신이 볼 수 있듯이레일 3 : 다형성 조건 쿼리

class Notification < ActiveRecord::Base 

belongs_to :notificatable, polymorphic: true 

end 

class BounceEmailNotification < ActiveRecord::Bas 

has_one :notification, :as => :notificatable, :dependent => :destroy 

end 

class UserNotifierEmailNotification < ActiveRecord::Base 

has_one :notification, :as => :notificatable, :dependent => :destroy 

end 

는 알림 유형 "반송 전자 메일 알림"또는 "사용자 통지 이메일 알림"이 될 수 있습니다. BounceEmailNotification 모델에는 이벤트 문자열 속성이 있습니다.으로 정렬 된 모든 사용자 알림 이메일 알림 및 특정 이벤트 값을 가진 모든 반송 이메일 알림을 검색하려면 어떻게해야합니까? 이 (사용 squeel) 같은

뭔가 :

(Notification.joins{ notificatable(BounceEmailNotification) }.where('bounce_email_notifications.event' => 'error') + Notification.joins { notificatable(UserNotifierEmailNotification) }).sort_by { |n| n.created_at } 

가 작동하지만 내가 알림을 주문 루비를 사용하지 않습니다. 내가 무엇을 할 수 있을지? 감사합니다

답변

0

보십시오.

Notification.joins('left outer join bounce_email_notifications on notifications.notificatable_id = bounce_email_notifications.id and notificatable_type = "BounceEmailNotification"'). 
where("bounce_email_notifications.event = ? or notificatable_type = ?",'error',UserNotifierEmailNotification.to_s). 
order('notifications.created_at') 

활성 기록 3.2을 사용하지만 난 squeel와 함께 사용하는 그러나 이것은 당신에게 간단한 아이디어를 줄 수있는 방법에 많은 squeel 캔트 코멘트를 사용하지 않은 3

이 레일에서 작동합니다 생각 . squeel에서

내가 docs

Notification.notificatable{notable(BounceEmailNotification).outer}. where{ 
     { ('bounce_email_notifications.event'=>'error') | (:notificatable_type=>UserNotifierEmailNotification.to_s) } 

개념 단계

에게
  1. 을 squeel를 사용하여이 같은 (테스트하지)가 생성 해낸 모든 bounce_email_notifications를 얻기 위해 bounce_email_notifications에 가입 왼쪽과 비 결과에서 bounce_email_notifications 알림

  2. bounce_email_notification s.event = 사건이 도움이 notifications.created

에서 희망 모든 UserNotifierEmailNotification 기록

  • 정렬 레코드 = 'UserNotifierEmailNotification'
    또는 notificatable_type.