2013-08-11 2 views
0

have have many events - 이벤트는 활동이 발생한 날짜와 시간입니다.미래에 적어도 일부 has_many 관계가있는 레코드를 선택하십시오.

내 활동 색인에 적어도 하나 이상의 일정이있는 활동을 나열하고 싶습니다.

나는 등의 카테고리로 빨아 내 다른 필터링을 수행 할 더듬다을 사용하고

어떻게 관계 전체의 큰 이상의 조건을 필터링 할 수 있습니다?

은 내가 할 수있을 것이라고 생각 :

obj = search 
     .joins(:event) 
     .where(:events => ['end_at > ?', Date.today ]) 

을하지만 난 오류 얻을 : 그래서 더 컨텍스트를 제공하기

undefined method `join' for #<Ransack::Search:0x007f8aa8eff918> 

UPDATE를 내 CONTROLER이

처럼 보인다
def index    
    if (params[:oldestchild].present? && params[:oldestchild].to_i > 0) || 
     (params[:youngestchild].present? && params[:youngestchild].to_i > 0) 
     @search = Activity 
     .where(' (oldest > ?z and youngest < ?) or (oldest > ? and youngest < ?) ', 
     params[:oldestchild], params[:oldestchild], 
     params[:youngestchild], params[:youngestchild], 
     ) 
     .search(params[:q]) 
    else 
     @search = Activity 
     .search(params[:q]) 
    end 

    @activities = Activity.filter(params, @search, request).includes(:provider).includes(:location) 

    if current_parent and current_parent.children.first 
     @min_child_age = current_parent.min_child_age 
     @max_child_age = current_parent.max_child_age 
    end 
end 

내 모델의 f ilter 방법 :

def self.filter(params, search, request)   
     obj = search.result(:distinct => true)  
#  obj = search 
#    .joins(:event) 
#    .where(:events => ['end_at > ?', Date.today ])  

    if params[:within].present? && (params[:within].to_i > 0) 
      obj = obj.where(:location_id => self.build_locations_array(request)) 
     end       
     if params[:q].present? and params[:q][:category_ids].present? 
      obj = obj 
      .joins(:categories) 
      .where('categories.id' => params[:q][:category_ids])                   
     end 
     if params[:date_range] == "This month"    
      obj = obj.where(:start => (Date.today)..(Date.today.at_end_of_month)) 
     end 

     if params[:date_range] == "Next month" 
      date = Date.today + 1.month    
      obj = obj.where(:start => (date.at_beginning_of_month)..(date.at_end_of_month))                  
     end 
     if params[:date_range] == "Next 3 Months"    
      date = Date.today + 3.month    
      obj = obj.where(:start => (Date.today)..(date.at_end_of_month))                  
     end 
     if params[:date_range] == "Whenever"    
      obj = obj.where("start > ?", Date.today)                  
     end 

     obj.paginate(:page => params[:page], :per_page => 5)    
    end 

UPATE 작업,하지만 매우 추한입니다

하나의 옵션은 SQL을 사용하는 것입니다. 검색 ([: Q] PARAMS)을 제거 :

obj = search.result(:distinct => true) 
.where("exists (select events.id from events 
      where events.activity_id = activities.id 
      and end_at > ?)", Date.today) 
+0

당신은 같은 더 몇 가지 세부 사항을 줄 수 곳이 코드 비트에서입니까? (모델 또는 컨트롤러?) – BroiSatse

+0

코드를 생성하지 않으면 SQL 솔루션뿐만 아니라 성능도 향상되지 않습니다. Activerecord는 EXISTS에 대한 지원으로 실제로 할 수 있습니다. –

답변

0

솔루션은 간단한 것 같다 다른 if 문에서와 Activity.filter 호출 후 사용 (또는 그들은 또한 같은 오류가 발생하는 경우 이후 포함).

또는, 어쩌면 더 나은 :

obj = search.result(:distinct => true).joins(:event).where(:events => ['end_at > ?', Date.today ]) 
+0

흠, 스 니펫은 내가 가진 것처럼, 이벤트가 아니라 이벤트 has_many 이벤트를 추측하기 때문에 "이벤트가 발견되지 않았습니다"라는 오류가 발생합니다. PG : Error : ERROR : 열 activities.events가 존재하지 않습니다. LINE 1 : ... "events". "activity_id"= "activities". "id"WHERE "activitie" – Will

관련 문제