复杂的rails查询

时间:2016-01-13 17:53:48

标签: ruby-on-rails activerecord

我不确定我在哪里出错了。 “主人”是所有工作的完整清单。然后我有三个过滤器用于不同的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 个答案:

答案 0 :(得分:1)

如果您拥有最新的Active Record:

,您应该可以这样做
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