复杂的ActiveRecord加入

时间:2016-02-13 06:03:51

标签: ruby-on-rails postgresql activerecord

我有一个名为invoicesinvoices belongs_to :subscriptionsubscription belongs_to :user的表格。 user has_many :licenses。我需要两个查询,其中一个查询获得在特定时间段内创建invoices的所有licenses,以便那些licenses没有特定的viewership_id

我能够正确地获得该查询,这是:

Invoice.joins(
  subscription: { 
    user: :licenses 
  } 
).where(
  # Other licenses during date range
  licenses: { 
    created_at: date_range
  }
).where.not(
  licenses: {
    viewership_id: Viewership.member.tier(1).first.id
  }
)

现在,我需要一个查询来获取在特定时间段内没有创建任何许可证的所有invoices。像这样的东西(尽管这不起作用):

Invoice.includes(
    subscription: { 
      user: :licenses 
    } 
  ).where(
    licenses: { 
      user_id: nil,
      created_at: date_range # <-- removing this works, but is an incorrect query, 
                             # because the user may have created other 
                             # licenses outside of this date_range and 
                             # that's alright--so querying without the 
                             # date_range means i may miss some users who 
                             # have licenses that just weren't created in 
                             # the date_range
    }
  )

1 个答案:

答案 0 :(得分:0)

可能会帮助你

javax.management.ReflectionException
    at sun.management.DiagnosticCommandImpl.invoke(DiagnosticCommandImpl.java:233)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:828)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$240(TCPTransport.java:683)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/1241281054.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:162)
    at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
    at javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(Unknown Source)
    at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.invoke(RMIConnector.java:1022)

获取在特定时间段内未创建任何许可证的所有# Create a date range from_date = 1.months.ago to_date = Time.now @invoices = Invoice.joins(subscription: [user: :licenses]).where("licenses.created_at BETWEEN ? AND ? AND licenses.viewership_id !=?", from_date, to_date, Viewership.member.tier(1).first.id)

invoices
相关问题