2016-01-13 76 views
0

我不知道我在哪里错了。 '主'是所有工作的完整列表。然后我为不同的door_manufacturer设置了三个过滤器。有些门票有parts_mfr,如果它与manufacturing_location相同,我需要提取。我很困惑。再次,我不知道我在这里错过了什么。复杂的钢轨查询

session[:door_location] = params[:location] || session[:door_location] || 'MASTER' 


if session[:door_location] == 'MASTER' 
    @tickets = Ticket.where(active: true, 
          complete_in_shop: false, 
          manufacturing_location: session[:factory]). 
    order(calendar_date: :asc).order(:calendar_order). 
    limit(90) 
else 

    tickets_door_location = Ticket.where(active: true, complete_in_shop: false, 
      manufacturing_location: session[:factory]) 
    tickets_parts_location = Ticket.where(active: true, complete_in_shop: false, 
      parts_mfr: session[:door_location]) 
    @tickets = (tickets_door_location << tickets_parts_location). 
      order(calendar_date: :asc).order(:calendar_order). 
      limit(90) 
end 

回答

1

你应该可以,如果你有最最新的活动记录要做到这一点,而不是:

tickets = Ticket.where(active: true, complete_in_shop: false) 
tickets = tickets.where(parts_mfr: session[:door_location]).or(tickets.where( 
      manufacturing_location: session[:factory])) 
@tickets = tickets.order(calendar_date: :asc).order(:calendar_order). 
      limit(90) 

我在一个旧版本的Rails的,所以我还没有一个有机会测试这个,但这里有一些例子:https://github.com/rails/rails/blob/master/activerecord/test/cases/relation/or_test.rb