2011-08-25 5 views
2

join을 수행하는 named_scope가 있습니다. 나는이 문제를 해결받을 수있는 방법이 있나요 내가 위의 줄을 실행named_scope 앨리어싱 문제에 참여하십시오.

named_scope :has_more_than_one,{ 
      :select  => "sessions.*", 
      :joins  => :attenders, 
      :conditions => {:attenders => {:attending => true}}, 
      :group  => "sessions.id", 
      :having  => "count(sessions.id) > 1" 


Meeting.has_more_than_one.all(:group => "sessions.id", 
    :include => [:attenders => [ :issues ]], 
    :conditions => ["sessions.id in (select attenders.session_id from attenders where person_id in (select persons.id from persons where first_name like (?) or last_name like (?) or first_name like (?) or last_name like (?)))", 
     "#{attendee_first_name}%","#{attendee_last_name}%","#{attendee_last_name}%","#{attendee_first_name}%"]) 

아래 named_scope 방법을 포함하고 앨리어싱 오류

ActiveRecord::StatementInvalid: Mysql::Error: Not unique table/alias: 'member_meetings' 

을 가지고있다 ..

답변

2

어딘가에 당신이 has_many이 something : through => : 여기에 보이지 않는 회원들. 나는 뭔가가 참석자라고 생각합니다.

어쨌든, 참여자를 범위 조인에 참여시킨 다음 : include 옵션에서 다시 참여하게되므로 동일한 테이블을 두 번 필요합니다.

그건 당신이 쿼리 내가 모두 member_meetings에게 & 참석자 별명이

:joins => "inner join member_meetings mm ON mm.meeting_id = meetings.id 
      inner join attendees at ON mm.attendee_id = at.id AND 
      at.attending is true" 

주처럼 보일 수 있습니다 조인이

named_scope :has_more_than_one,{ 
     :select  => "sessions.*", 
     :joins  => <specify the join with the condition here aliasing the tables>, 
     # this line would go away :conditions => {:attenders => {:attending => true}}, 
     :group  => "sessions.id", 
     :having  => "count(sessions.id) > 1" 

같은 범위를 정의해야 맞습니다합니다.